将单个BaseAdapter用于多个ListView

时间:2013-02-20 17:12:41

标签: android android-listview android-adapter android-adapterview

我分别在三个标签A,B和C中有三个ListView。所有这些都从公共适配器类中获取数据。但是,当我添加搜索选项并尝试在任何选项卡中更改列表时出现问题,例如tabA。一旦我完成并切换到tabB,tabA中加载的数据也会显示在tabB中。

我知道这是因为对所有列表视图使用了一个BaseAdapter类。通过互联网进行繁琐的搜索,我找到了 this link 。这是具有更多视图类型的ViewHolder模式的限制。

我希望我没有混淆现场。任何人都可以提供更好的出路吗?

我们如何使用单个适配器处理多个视图?

修改

我有三个选项卡 - A,B,C。这些选项卡中的每一个都是一个ActivityGroup类。 TabC包含一个listView。 TabA和TabB具有转到TabC的按钮。

tabC中类的XML文件(这是来自Tab A 的请求)

<LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

       <ListView
        android:id="@+id/enquiryListViewA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="7dp" >
</LinearLayout>

tabC中的目标类是

public class TestClass extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if(0 == Constants.requestID) { //requestID is used to identify the tab
             //request from tab A
             setContentView(R.layout.custom_fullpicture_A);
         } else { //request from tab B
             setContentView(R.layout.custom_fullpicture_B);
         }
}

在Handler类中,我正在设置适配器。在这里,我还根据选项卡类型

检查有效的listView
 if(0 == Constants.requestID) {
   ListView enquiryView = (ListView) ((Activity) childContext).findViewById(R.id.enquiryListViewA);
   adapterA = new CustomFullPictureAdapter(childContext, msg.obj);
   enquiryView.setAdapter(adapterA);
 } else {
   ListView enquiryView = (ListView) ((Activity) childContext).findViewById(R.id.enquiryListViewB);
   adapterB = new CustomFullPictureAdapter(childContext, msg.obj);
   enquiryView.setAdapter(adapterB);
 } 

adpater类的getView方法是

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    System.out.println("Full Picture adapter: CONVERT VIEW : :: " + convertView);
    System.out.println("Data count from DB :: " + getCount());
    int viewType = Constants.requestID;

    View viewAccounts = null;
    View viewCM = null;
    View view = null;
    View masterView = null;

    if(viewType == 0) {
        viewAccounts = convertView;
    } else if(viewType == 1) {
        viewCM = convertView;
    } else {
        view = convertView;
    }


    if(viewAccounts == null) {
        holder = new ViewHolder();
         if(!Constants.isLoggedInCustomer) { //logged in as user
             viewAccounts = mInflater.inflate(R.layout.custom_fullpicturerow_A, null);

            holder.salesCostAcc = (TextView) viewAccounts.findViewById(R.id.vSalesCostAcc);
            holder.profitAcc = (TextView) viewAccounts.findViewById(R.id.vProfitAcc);
             holder.gpAcc = (TextView) viewAccounts.findViewById(R.id.vGpAcc);
             holder.salesValueAcc = (TextView) viewAccounts.findViewById(R.id.vSalesValueAcc);
             holder.quantityAcc = (TextView) viewAccounts.findViewById(R.id.vQuantityAcc);
             holder.transactionCountAcc = (TextView) viewAccounts.findViewById(R.id.vTransAcc);
             holder.avgPriceAcc = (TextView) viewAccounts.findViewById(R.id.vAvgPriceAcc);
             holder.objectNameAcc = (TextView) viewAccounts.findViewById(R.id.vObjectNameAcc);
             holder.objectCodeAcc = (TextView) viewAccounts.findViewById(R.id.vObjectCodeAcc);

             holder.salesValueAcc.setText(searchArrayList.get(position).getSalesValue());
             holder.quantityAcc.setText(searchArrayList.get(position).getQuantity());
             holder.transactionCountAcc.setText(searchArrayList.get(position).getTransactionCount());
             holder.avgPriceAcc.setText(searchArrayList.get(position).getAvgPrice());     
             holder.objectNameAcc.setText(searchArrayList.get(position).getObjectName());
             holder.objectCodeAcc.setText(searchArrayList.get(position).getObjectCode());

             holder.salesCostAcc.setText(searchArrayList.get(position).getSalesCost());
            holder.profitAcc.setText(searchArrayList.get(position).getProfit());
             holder.gpAcc.setText(searchArrayList.get(position).getProfitMargin());

             masterView = viewAccounts;

             viewAccounts.setTag(holder);
         } else {

         }
    } 



    if(view == null) {
        holder = new ViewHolder();
         if(!Constants.isLoggedInCustomer) { //logged in as user
             viewCM = mInflater.inflate(R.layout.custom_fullpicturerow_B, null);

            holder.salesCost = (TextView) viewCM.findViewById(R.id.vSalesCost);
            holder.profit = (TextView) viewCM.findViewById(R.id.vProfit);
             holder.gp = (TextView) viewCM.findViewById(R.id.vGp);
             holder.salesValue = (TextView) viewCM.findViewById(R.id.vSalesValue);
             holder.quantity = (TextView) viewCM.findViewById(R.id.vQuantity);
             holder.transactionCount = (TextView) viewCM.findViewById(R.id.vTrans);
             holder.avgPrice = (TextView) viewCM.findViewById(R.id.vAvgPrice);
             holder.objectName = (TextView) viewCM.findViewById(R.id.vObjectName);
             holder.objectCode = (TextView) viewCM.findViewById(R.id.vObjectCode);

             holder.salesValue.setText(searchArrayList.get(position).getSalesValue());
             holder.quantity.setText(searchArrayList.get(position).getQuantity());
             holder.transactionCount.setText(searchArrayList.get(position).getTransactionCount());
             holder.avgPrice.setText(searchArrayList.get(position).getAvgPrice());     
             holder.objectName.setText(searchArrayList.get(position).getObjectName());
             holder.objectCode.setText(searchArrayList.get(position).getObjectCode());

             holder.salesCost.setText(searchArrayList.get(position).getSalesCost());
            holder.profit.setText(searchArrayList.get(position).getProfit());
             holder.gp.setText(searchArrayList.get(position).getProfitMargin());

             masterView = viewCM;

             viewCM.setTag(holder);
         } else {

         }
    }

    if(null != masterView)
    holder = (ViewHolder) masterView.getTag();

    if(0 == position % 2)
        masterView.setBackgroundResource(R.color.textColorWhite);
    else
        masterView.setBackgroundResource(R.color.textColorCyan);

    return masterView;
}

所以这就是发生的事情。用户单击TabA或B到TabC的按钮。这会将TestClass添加到选项卡的当前活动。使用requestID变量,适配器为TabA和TabB中的TestClass提供不同的数据。但是,如果我在标签之间切换,它们都具有相同的值!

希望我没有混淆。如果您有疑问,请询问。您认为阻止什么?

0 个答案:

没有答案