0

I have three ListViews in three tabs A, B and C respectively. All of them obtain data from a common adapter class. However, problem arises when I add search options and try to change list in any tab, say tabA. As soon as I have done it and switch to tabB, the data loaded in tabA is also shown in tabB.

I recognize this is due to using a single BaseAdapter class for all the list Views. Upon tedious searching over the internet, I found this link . This is a limitation to ViewHolder pattern with more view types.

I hope I did not confuse the scene. Could anyone provide a better way out?

How can we handle multiple views with single adapter?

EDIT:

I have three tabs- A, B, C. Each of these tabs is an ActivityGroup class. TabC contains a listView. TabA and TabB has buttons to go to TabC.

XML file of a class in tabC (This is for request coming from 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>

the target class in tabC is

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);
         }
}

And in the Handler class, I am setting adapter. Here also I check the valid listView depending on the tab type

 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);
 } 

And the getView method of adpater class is

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;
}

So here is what happens. User clicks the button from TabA or B to TabC. This will add TestClass to current activity of the tab. Using requestID variable, adapter provides different data to TestClass in TabA and TabB. However, if I switch between tabs, all of them have the same value!

Hope I did not confuse. Please ask in case you have queries. What do you think is blocking?

Renjith
  • 3,457
  • 5
  • 46
  • 67
  • 1
    Why do you want to use one Adapter for three ListViews? It seems easier to use three copies of the same Adapter to point to one data set. – Sam Feb 20 '13 at 17:14
  • I do not know which is the right approach. I thought of having different adapter classes for three views. But then, their contents would be exactly the same with different component variable names..! Is it a good design approach? – Renjith Feb 20 '13 at 17:18
  • You only need one Adapter class, but call `new MyAdapter()` for each ListView. This way when you filter the Adapter in Tab A the other Tabs are not changed. – Sam Feb 20 '13 at 17:23
  • It did not work that way, Sam! I created new adapter instances for each ListViews. I have different layout XML for each ListView too. – Renjith Feb 20 '13 at 17:32
  • It's impossible for me to know what you have tried. Please post your relevant code. – Sam Feb 20 '13 at 17:35
  • It seems your problems are more likely related to the data store sharing the data and not the adapter class. As Sam already said, nobody can really help you if you don't add the relevant code you used in your question. – user Feb 20 '13 at 18:06
  • How did you solve this?? I'm facing similar problem – carefree Feb 23 '15 at 07:55
  • I was sharing the same data across tabs resulting in app crash. Modified the adapter and that helped! – Renjith Feb 23 '15 at 09:24

0 Answers0