0

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

我认识到这是由于对所有列表视图使用单个 BaseAdapter 类。在互联网上进行了繁琐的搜索后,我找到了这个链接。这是具有更多视图类型的 ViewHolder 模式的限制。

我希望我没有混淆场景。谁能提供更好的出路?

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

编辑

我有三个选项卡 - A、B、C。每个选项卡都是一个 ActivityGroup 类。TabC 包含一个列表视图。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 提供不同的数据。但是,如果我在选项卡之间切换,它们都具有相同的值!

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

4

0 回答 0