• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Issue with Custom ListView on Tabs

albertlse

Lurker
Hi,

I am new to Android programming.

I created an Activity which has 2 Tabs: Queue Tab and ActiveProcess Tab.

Each tab contains 1 Custom ListView.

The Custom ListView extends from BaseAdapter.

Each item in the Custom ListView has 2 TextView: Label1 and Label2.

The data displayed on this 2 Custom ListView are different, but their layout will be the same.

Here are my codes:

list_view_item.xml:

Code:
<?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" >
    
	<TextView
		android:id="@+id/Label1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textColor="#FF0000"		
		/>
	
	<TextView
		android:id="@+id/Label2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		/>
	
</LinearLayout>

activity_summary.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" xmlns:android1="http://schemas.android.com/apk/res/android">
        
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <RelativeLayout
                    android:id="@+id/tabQueue"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" > 
                    
                    <ListView
				        android:id="@+id/lsvQueue"
				        android:layout_width="match_parent"
				        android:layout_height="wrap_content" />
                    
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/tabActiveProcess"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                    
                    <ListView
				        android:id="@+id/lsvActiveProcess"
				        android:layout_width="match_parent"
				        android:layout_height="wrap_content" />
                    
                </RelativeLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

CustomBaseAdapter.java:

Code:
package com.albertlse.mq;

import java.util.ArrayList;

import com.albertlse.mq.model.ListViewItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomBaseAdapter extends BaseAdapter {
	private static ArrayList<ListViewItem> listItems = new ArrayList<ListViewItem>();
	 
    private LayoutInflater mInflater;
 
    public CustomBaseAdapter(Context context, ArrayList<ListViewItem> item) {
    	listItems = item;
        mInflater = LayoutInflater.from(context);
    }
 
    public int getCount() {
        return listItems.size();
    }
 
    public Object getItem(int position) {
        return listItems.get(position);
    }
 
    public long getItemId(int position) {
        return position;
    }
     
	public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_view_item, null);
            holder = new ViewHolder();
            holder.Label1 = (TextView) convertView.findViewById(R.id.Label1);
            holder.Label2 = (TextView) convertView.findViewById(R.id.Label2);
 
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        holder.Label1.setText(listItems.get(position).getLabel1());
        holder.Label2.setText(listItems.get(position).getLabel2());
 
        return convertView;
    }
 
    static class ViewHolder {
        TextView Label1;
        TextView Label2;
    }
}

SummaryActivity.java:

Code:
package com.albertlse.mq;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.albertlse.mq.model.ListViewItem;
import com.albertlse.mq.model.Queue;
import com.albertlse.mq.webservice.CallerSummaryActivity;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TabHost.TabSpec;

public class SummaryActivity extends Activity {
	
	public static String result = "";    /** Called when the activity is first created. */
	
	public static String jsonQueue = ""; /* Queue results from Webservice in JSON format */
	public static String jsonActiveProcess = ""; /* ActiveProcess results from Webservice in JSON format */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_summary);
		
		final AlertDialog ad = new AlertDialog.Builder(this).create();
		
		setupTabs();
			
		loadTabs();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.summary, menu);
		return true;
	}
	
	private void setupTabs() {
		TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();
		
		TabSpec spec1 = tabHost.newTabSpec("tabQueue");
		spec1.setContent(R.id.tabQueue);
		spec1.setIndicator("Queues");
		tabHost.addTab(spec1);
		
		TabSpec spec2 = tabHost.newTabSpec("tabActiveProcess");
		spec2.setContent(R.id.tabActiveProcess);
		spec2.setIndicator("Active Process");
		tabHost.addTab(spec2);
	}
	
	private void loadTabs() {
		try
    	{
	    	result = "START";
	    	
	    	/* Connect to Webservice to get results */
	    	
            CallerSummaryActivity caller = new CallerSummaryActivity(); 
            caller.QueueManager = this.getResources().getString(R.string.QueueManager);
            caller.QueueName = this.getResources().getString(R.string.QueueName);
            caller.ChannelName = this.getResources().getString(R.string.ChannelName);
            caller.ConnectionName = this.getResources().getString(R.string.ConnectionName);
            
            caller.join(); 
            caller.start();
            
            while(result == "START") {
                try {
                    Thread.sleep(10); 
                }
                catch (Exception ex) {
                }
            }
            
            loadQueueTab();
            
            loadActiveProcessTab();
    	}
	    catch (Exception ex) 
	    {
	    	
        }
	}
	
	private void loadQueueTab() {
		try
		{
            JSONArray jArray = new JSONArray(jsonQueue);
            ArrayList<ListViewItem> queueItems = new ArrayList<ListViewItem>();
            
            for(int i = 0; i < jArray.length(); i++){
                JSONObject obj = jArray.getJSONObject(i);
                
                ListViewItem queueItem = new ListViewItem();
                queueItem.setLabel1(obj.getString("Description"));
                queueItem.setLabel2(obj.getString("QueueName"));
                queueItems.add(queueItem);
            }
            
            ListView lsvQueue = (ListView)findViewById(R.id.lsvQueue);            
            lsvQueue.setAdapter(new CustomBaseAdapter(this, queueItems));
            
            lsvQueue.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                	CustomBaseAdapter ca = (CustomBaseAdapter)parent.getAdapter();
                	ListViewItem queueItem = (ListViewItem)ca.getItem(position);

                    Intent i = new Intent(SummaryActivity.this, DetailActivity.class);
                    i.putExtra("QueueName", queueItem.getLabel2());
                    startActivity(i);
                }
            });
		} catch (Exception ex) 
		{
			
		}
	}	

	private void loadActiveProcessTab() {
		try
		{
            JSONArray jArray = new JSONArray(jsonActiveProcess);
            ArrayList<ListViewItem> processItems = new ArrayList<ListViewItem>();
            
            for(int i = 0; i < jArray.length(); i++){
                JSONObject obj = jArray.getJSONObject(i);
                
                ListViewItem processItem = new ListViewItem();
                processItem.setLabel1(obj.getString("Process"));
                processItem.setLabel2(obj.getString("LastRunTime"));
                processItems.add(processItem);
            }
            
            ListView lsvActiveProcess = (ListView)findViewById(R.id.lsvActiveProcess);            
            lsvActiveProcess.setAdapter(new CustomBaseAdapter(this, processItems));
		} catch (Exception ex) 
		{
			
		}
	}
}

When the Project run, it will connect to Webservices to get the Queue results and ActiveProcess results.

Upon getting the results, it will populate the results into respective Custom ListView in respective Tabs.

The ListView in Queue Tab is supposed to show Queue results, and ActiveProcess Tab is supposed to show ActiveProcess results.

But now the problem I encountered is, both the Custom ListView in that 2 Tabs are showing the same results. Both Queue Tab and ActiveProcess Tab are showing ActiveProcess results, even though the Custom ListView in these 2 Tabs are of different ID.

I am wondering how could this happened? Is there anything which I have coded wrongly?

I tried to created 2 CustomBaseAdapters, CustomBaseAdapterA for the ListView in Queue Tab, and CustomBaseAdapterB for the ListView in Queue Tab.
Even though, this approach will resolve the problem, but I think this is not a very good solution, as both CustomBaseAdapterA and CustomBaseAdapterB have the same codes.

Can I share 1 CustomBaseAdapter for 2 seperate ListView?

Please advise.

Thank you.
 
Back
Top Bottom