Extending fragments using a list

I work with fragments in android, extend android.app.ListFragment to display a listview, but it only takes up one element in the list view. I want to display 2 elements as a list, this is possible with the extension of the ListActivity class, but I want to expand both the fragment and listactivty.
Send this image

public class ListFragmentnewforel extends android.app.ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayList<HashMap<String, String>> list = buildData(); String[] from = { "purpose" , "name","desc", "num"}; int[] to = { R.id.detailsText1, R.id.detailsText2, R.id.detailsText3, }; SimpleAdapter adapter = new SimpleAdapter(this, list,R.layout.mylistview,from,to); setListAdapter(adapter); } public void onListItemClick(ListView l, View v, int position, long id) { ArrayList<String>arr=new ArrayList<String>(); String item = (String) getListAdapter().getItem(position); DetailFragment fragment = (DetailFragment) getFragmentManager() .findFragmentById(R.id.detailFragment); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { fragment.setText(item); } else { Intent intent = new Intent(getActivity().getApplicationContext(), DetailActivity.class); Xmlparsingactiforele d=new Xmlparsingactiforele(); arr=d.myelarraylist(item); // arr.get(0); intent.putExtra("value1", arr.get(0)); intent.putExtra("value2", arr.get(1)); intent.putExtra("value3", arr.get(2)); intent.putExtra("value4", arr.get(3)); startActivity(intent); } } private ArrayList<HashMap<String, String>> buildData() { ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); list.add(putData("Android", "Mobile","two", "1")); list.add(putData("Windows7", "Windows7","one", "2")); list.add(putData("iPhone", "iPhone","three", "3")); return list; } private HashMap<String, String> putData(String name, String purpose,String a, String n) { HashMap<String, String> item = new HashMap<String, String>(); item.put("name", name); item.put("purpose", purpose); item.put("desc", a); item.put("num", n); return item; } } 
+4
source share
2 answers

Nothing prevents you from showing everything you want in a ListView element. Please read the ListFragment API ListFragment .

You can create a ListAdapter that returns a View the type you want to use in the getView method. In this regard, there is hardly any difference between ListActivity and ListFragment .

+2
source

It is impossible to expand both a fragment and activity classes to one class. Thus, extends Activity to the main class and creates an inner class and extends the Fragment class.

0
source

Source: https://habr.com/ru/post/1433775/


All Articles