Android - 多个列表未显示

时间:2010-08-04 11:50:26

标签: android listview listadapter

我有两个需要处理和显示的列表,我使用了以下技术 -

我从两个来源获取数据,处理它并拥有2个自定义适配器,它们获取数据并将其放在relvant视图中。

但是我只能查看一个列表(视图xml顶部的列表) - 而不是另一个列表,

有人可以帮我解决这个问题。它让我疯了。

主要活动代码belwo:

   public class TestList extends Activity {
   private final static String TAG = "ListSample";
   private List<String> items;
   private List<String> items2;
   private CustomTeamListAdapter m_adapter;
   private CustomTeamListAdapterTwo t_adapter;

   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      items = new ArrayList<String>();
      for (int i = 0; i < 20; i++) {
         items.add(i + "|||>>");
      }
      ListView list = (ListView) findViewById(R.id.customlist);
      this.m_adapter = new CustomTeamListAdapter(this, R.layout.listitem,
            (ArrayList<String>) items);
      list.setAdapter(m_adapter);


      /******************************************************************************/

      items2 = new ArrayList<String>();
      for (int i = 0; i < 20; i++) {
         items2.add(i + ">>----");
      }

      ListView list_two = (ListView) findViewById(R.id.customlisttwo);
      this.t_adapter = new CustomTeamListAdapterTwo(this, R.layout.listitem,
            (ArrayList<String>) items2);
      list_two.setAdapter(t_adapter);

   }


   private class CustomTeamListAdapter extends ArrayAdapter<String> {

      private ArrayList<String> items;

      public CustomTeamListAdapter(Context context, int textViewResourceId,
            ArrayList<String> items) {
         super(context, textViewResourceId, items);
         this.items = items;
      }

      public View getView(int position, View convertView, ViewGroup parent) {
         View v = convertView;
         LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(R.layout.listitem, null);

         String o = items.get(position);
         if (o != null) {

            TextView tt = (TextView) v.findViewById(R.id.sampletext);
            tt.setText(o + "FirstSet");
            tt.setId(position);
         }
         return v;
      }
   }

   private class CustomTeamListAdapterTwo extends ArrayAdapter<String> {

      private ArrayList<String> items;

      public CustomTeamListAdapterTwo(Context context, int textViewResourceId,
            ArrayList<String> items) {
         super(context, textViewResourceId, items);
         this.items = items;
      }

      public View getView(int position, View convertView, ViewGroup parent) {
         View v = convertView;
         LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(R.layout.listitem, null);

         String o = items.get(position);
         if (o != null) {

            TextView tt = (TextView) v.findViewById(R.id.sampletext);
            tt.setText(o + "SecondSet");
            tt.setId(position);
         }
         return v;
      }
   }

}

下面的主要布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ListView android:id="@+id/customlisttwo"
      android:layout_width="fill_parent" android:layout_height="wrap_content" />

   <TextView android:id="@+id/randomtext" android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:background="#CCCCCC"
      android:text="Random Text" />

   <ListView android:id="@+id/customlist" android:layout_width="fill_parent"
      android:layout_height="wrap_content" />

</LinearLayout>

项目布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ListView android:id="@+id/customlisttwo"
      android:layout_width="fill_parent" android:layout_height="wrap_content" />

   <TextView android:id="@+id/randomtext" android:layout_width="fill_parent"
      android:layout_height="wrap_content" android:background="#CCCCCC"
      android:text="Random Text" />

   <ListView android:id="@+id/customlist" android:layout_width="fill_parent"
      android:layout_height="wrap_content" />

</LinearLayout>

提前感谢您的想法

1 个答案:

答案 0 :(得分:0)

尝试使用Mark Murphy的https://github.com/commonsguy/cwac-merge合并适配器。您无法在同一活动中拥有两个列表视图。您可以在两个布局之间添加该文本视图。