Reputation: 134
To tell my problem I want to explain on an example
Lets say we have 12 arrays, lets call them List1 to List12 and each of them has different sizes.
I want to show each 3 lists in one screen -I have 4 screens-, so I created an xml for list items and a new Adapter (MyAdapter) which extends ArrayAdapter. I also solved the problem for showing them in one screen by using if statements.
But these 4 screens has different sizes and even if I control ArrayIndexOutOfBoundsException
problem, the lists has empty rows as I wrote
MyAdapter<String> adapter = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.MyListView,
List1)
Do i have to write 4 adapters as
MyAdapter<String> adapter1 = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.MyListView,
List1)
MyAdapter<String> adapter2 = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.MyListView,
List4)
MyAdapter<String> adapter3 = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.MyListView,
List7)
MyAdapter<String> adapter4 = new MyAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.MyListView,
List10)
or is there a simpler solution?
Thank you for your helps!
Myadapter class:
public class MyAdapter<String> extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] listname) {
super(context, resource, textViewResourceId, listname);
}
public MyAdapter(MainActivity mainActivity, int simpleSpinnerItem,
String[] listlist) {
super(mainActivity, simpleSpinnerItem, listlist);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
TextView tv1 = (TextView) row.findViewById(R.id.textview1);
TextView tv2 = (TextView) row.findViewById(R.id.textview2);
TextView tv3 = (TextView) row.findViewById(R.id.textview3);
if (ListID == 1) {
tv1.setText(List1[position]);
if (position < List3.length) {
tv2.setText(List2[position]);
tv3.setText(List3[position]);
} else if (position < List2.length) {
tv2.setText(List4[position]);
}
} else if (ListID == 2) {
...
}
else
tv1.setText(list0[position]);
return row;
}
}
Length of lists List1 > List2 > List3
Two constructor is just because I have a spinner on screen to go between screens.
ListID is the spinners position.
Padma Kumar's solution worked by adding this. And I used default Adapter for Spinner, which solves my problem.
@Override
public int getCount() {
if (ListID == 1) {
return List1.length;
} else if (ListID == 2) {
return List4.length;
} else if (ListID == 3) {
return List7.length;
} else if (ListID == 4) {
return List10.length;
}
}
Upvotes: 1
Views: 656
Reputation: 3785
I'm not sure I understand your question, but if you are trying to use different lists on your adapter you should make sure that you call notifyDataSetChanged
on your adapter after you modify the underlying List object. For example, add something like the code bellow to your custom adapter.
public void swapList(List<String> list) {
mList = list;
this.notifyDataSetChanged();
}
Whenever you want to show a different list on your list view simply call this from your code:
mAdapter.swapList(ListX);
Upvotes: 1
Reputation: 20041
I think you are hardcoding your getcount method make it as below.
public int getCount() {
return list.size();
}
it will return the number of length in your list that you passed.
so that you will never get any empty rows.
Upvotes: 1