4

I am working with fragments in android,am extending android.app.ListFragment to display listview, but it takes only one item in a list view. I want to display 2 items in a list view ,its possible with extending ListActivity class, but i want to extend both fragment and listactivty.
Refer 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

2 回答 2

2

没有什么能阻止你在项目中展示你想要的任何东西ListView。请阅读 API 文档ListFragment

您可以创建一个在其方法ListAdapter中返回所需类型的 。在这方面,和之间几乎没有任何区别。ViewgetViewListActivityListFragment

于 2012-09-12T11:21:08.450 回答
0

不可能将片段和活动类扩展到单个类。因此,将 Activity 扩展到主类并创建内部类并将 Fragment 类扩展到其中。

于 2012-09-12T11:08:29.633 回答