无法使用单个BaseAdapter来提供多个listView

时间:2013-02-21 11:33:05

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

我昨天在这里发布了question。由于我没有添加适当的代码片段,我几乎没有解决方案。所以我在这里发布全新内容。只是为了保持积极的问题。

我有三个选项卡 - 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提供不同的数据。但是,如果我在标签之间切换,它们都具有相同的值!

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

1 个答案:

答案 0 :(得分:1)

AFAIK,您正在使用单个集合,即searchArrayList,我可以在您发布的代码中看到。

您必须正确维护其状态才能解决问题。