使用listactivity扩展片段

时间:2012-09-12 11:04:35

标签: android android-fragments listactivity

我正在使用android中的片段,我正在扩展android.app.ListFragment以显示listview,但它只需要列表视图中的一个项目。我想在列表视图中显示2个项目,它可以扩展ListActivity类,但我想扩展fragment和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;
      }
}  

2 个答案:

答案 0 :(得分:2)

没有什么可以阻止你在ListView项目中显示你想要的任何内容。请阅读ListFragment的API文档。

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

答案 1 :(得分:0)

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