How to set up custom ListView in android Fragement?

I am making an application for an Android tablet. In this case, I need to display two lists. One for a simple list and one for a custom list. As soon as I click on a simple list line, then these details should be sent to another user list. For this, I took a fragment to display both lists in one screen. For a custom ListView, I was adapted to a custom adapter to bind custom data. But when I am in a simple list line, the application shows an unresponsive error. My code will be like this. My snippet contains code like this

public class listDetails extends Fragment{ private int nAndroids; public static ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); ListDetailsAdapter adapter; static final String KEY_Title = "title";//"item"; public listDetails() { } /** * Constructor for being created explicitly */ public listDetails(int nAndroids) { this.nAndroids = nAndroids; } /** * If we are being created with saved state, restore our state */ @Override public void onCreate(Bundle saved) { super.onCreate(saved); if (null != saved) { nAndroids = saved.getInt("nAndroids"); } } /** * Save the number of Androids to be displayed */ @Override public void onSaveInstanceState(Bundle toSave) { toSave.putInt("nAndroids", nAndroids); } /** * Make a grid and fill it with n Androids */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) { int n; Context c = getActivity().getApplicationContext(); LinearLayout l = new LinearLayout(c); String listitems[]=new String[nAndroids]; HashMap<String, String> map = new HashMap<String, String>(); map.put(KEY_Title, "Question1"); map = new HashMap<String, String>(); map.put(KEY_Title, "Question2"); map = new HashMap<String, String>(); map.put(KEY_Title, "Question3"); map = new HashMap<String, String>(); map.put(KEY_Title, "Question4"); menuItems.add(map); for (n = 0; n < nAndroids; n++) { listitems[n] = "one"+n; ListView list = new ListView(c); adapter = new ListDetailsAdapter(this, menuItems); list.setAdapter(adapter); } return l; } 

}

And my custom adapter code like this

 public class ListDetailsAdapter extends BaseAdapter{ ///private listDetails listactivity; private Activity activity; private ArrayList<HashMap<String, String>> data; private static LayoutInflater inflater=null; public ListDetailsAdapter(Activity a, ArrayList<HashMap<String, String>> d) { activity = a; data=d; inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { // TODO Auto-generated method stub return data.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View vi=convertView; if(convertView==null) vi = inflater.inflate(R.layout.customlist, null); TextView title = (TextView)vi.findViewById(R.id.txtTitle); Button btnone = (Button)vi.findViewById(R.id.btnfirst); Button btntwo = (Button)vi.findViewById(R.id.btnsecond); Button btnthree = (Button)vi.findViewById(R.id.btnthird); Button btnfour = (Button)vi.findViewById(R.id.btnfourth); Button btnfive = (Button)vi.findViewById(R.id.btnfifth); btnone.setOnClickListener(oneclick); btntwo.setOnClickListener(twoclick); btnthree.setOnClickListener(thrirdclick); HashMap<String, String> mymap = new HashMap<String, String>(); mymap = data.get(position); title.setText(mymap.get(listDetails.KEY_Title)); return vi; } private View.OnClickListener oneclick = new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Log.e("button clicked", "button one clicked"); } }; 

}

Please tell me what is going wrong in the above code.

+6
source share
1 answer

i a fragment was taken to display both lists in one screen. It is possible that the fragment is problem.one for one list view and display two fragments in one action. I think it is much better. Because I don't think this snippet is supported for loading multiple layouts (listviews)

0
source

All Articles