I have two listviews on one activity, everything is working fine, but if I want to get the clicked items from listview 1 and listview 2 I can only get the items from listview 1
activity_smokes:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollojt"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
>
<ListView
android:id="@+id/listView1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_weight=".5">
</ListView>
<ListView
android:layout_width="0px"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_weight=".50"
android:layout_marginTop="100dp"
/>
</LinearLayout>
</ScrollView>
smokes:
CustomAdapter adapter, adapterCT;
public smokes CustomListView = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();
public ArrayList<ListModel> CustomListViewValuesArrCT = new ArrayList<ListModel>();
String[] Difficulties = {"Easy", "Medium", "Hard", "Very Hard"};
String Map;
String value;
private enum Maps {
Cobblestone, Chache, Dust, Inferno, Mirage, Overpass, Train;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smokes);
Map = getIntent().getStringExtra("Map");
Maps abc= Maps.valueOf(Map);
if(Map.equals("Dust")){
getSupportActionBar().setTitle("Dust 2");
} else {
getSupportActionBar().setTitle(Map);
}
CustomListView = this;
switch(Map) {
case Cobblestone:
setListData()
}
Resources res =getResources();
ListView listCT = (ListView)findViewById(R.id.listView);
adapterCT=new CustomAdapter(CustomListView, CustomListViewValuesArrCT,res);
listCT.setAdapter( adapterCT);
listCT.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListModel tempValuesCT = (ListModel) CustomListViewValuesArrCT.get(position);
Toast.makeText(smokes.this,
"" + tempValuesCT.getSmokeName(),
Toast.LENGTH_LONG)
.show();
}
});
ListView list= ( ListView )findViewById( R.id.listView1 ); // List defined in XML ( See Below )
/**************** Create Custom Adapter *********/
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListModel tempValues = (ListModel) CustomListViewValuesArr.get(position);
Toast.makeText(smokes.this,
"" + tempValues.getSmokeName(),
Toast.LENGTH_LONG)
.show();
}
});
}
public void setListData()
{
ListModel lmT = new ListModel("Banana to CT Cross", "i0", Difficulties[0]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Banana to Coils","i1",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Second Mid to Long","i3",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
lmT = new ListModel("Second Mid to Short","i4",Difficulties[1]);
CustomListViewValuesArr.add(lmT);
ListModel lmCT = new ListModel("A Pit to Short", "i2", Difficulties[1]);
CustomListViewValuesArrCT.add(lmCT);
lmCT = new ListModel("A Long to Mid", "i5", Difficulties[1]);
CustomListViewValuesArrCT.add(lmCT);
}
/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
static class CustomAdapter extends BaseAdapter {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = (LayoutInflater)activity.
getSystemService(LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public ImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if(convertView==null){
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
vi = inflater.inflate(R.layout.tabitem, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.text1=(TextView)vi.findViewById(R.id.text1);
holder.image=(ImageView)vi.findViewById(R.id.imageView);
/************ Set holder with LayoutInflater ************/
vi.setTag( holder );
}
else
holder=(ViewHolder)vi.getTag();
if(data.size()<=0)
{
holder.text.setText("No Data");
holder.text1.setText("No Data");
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = ( ListModel ) data.get( position );
/************ Set Model values in Holder elements ***********/
holder.text.setText( tempValues.getSmokeName() );
holder.text1.setText( tempValues.getDifficulty() );
}
return vi;
}
}
}
EDIT: I updated the code, it's working now for me!
The reason is that you are using different datasets to populate your listviews but in onItemClick
you are fetching the data from only one of the datasets although you are selecting an item from any of the listviews.
First List uses "CustomListViewValuesArrCT" dataset
ListView listCT = (ListView)findViewById(R.id.listView);
adapterCT=new CustomAdapter(CustomListView, CustomListViewValuesArrCT,res);
listCT.setAdapter( adapterCT);
Second List uses "CustomListViewValuesArr" dataset
ListView list= ( ListView )findViewById( R.id.listView1 );
adapter=new CustomAdapter( CustomListView, CustomListViewValuesArr,res );
list.setAdapter(adapter);
BUT Fetching data only from "CustomListViewValuesArr" in either list item selection .
public void onItemClick(int mPosition)
{
// You are fetching value from "CustomListViewValuesArr" only
ListModel tempValues = ( ListModel ) CustomListViewValuesArr.get(mPosition);
.....
}
You need to fix your flow so that upon list item click you fetch the data from that dataset using which you set the adapter for that particular list.