So here's the problem. I'm writing an application that queries a web-service for some data. The returned data is an arbitrary number of strings.
I need to display a list of these strings, which obviously is most easily done by nesting a ListView in an other ListView. That is making a ListView whose rows contain other ListViews. The problem I've run into is, that when I do this, the inner ListViews only display the first element in the backing array.
I'm using a custom ListAdapter that extends BaseAdapter. Using the Eclipse debugger I've been able to determine that the getView(int,View,viewGroup)-method for each instance (one for each row in the nesting ListView) gets always called three times, but always with the same int-argument (that is 0). The number of times the getView() for a ListAdapter gets called seems to have little to do with the number of elements the backing array contains.
I've created a fairly simple test application that illustrates what I'm actually doing.
This is my ListAdapter. An other ListAdapter, the nested InnerListAdapter is used to populate the inner lists.
package org.my.tests;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class NestedListAdapter extends BaseAdapter {
Context context;
List<List<String>> list;
public NestedListAdapter(Context context, List<List<String>> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListView view = new ListView(context);
view.setAdapter(new InnerListAdapter(list.get(position)));
return view;
}
public class InnerListAdapter extends BaseAdapter {
List<String> list;
public InnerListAdapter(List<String> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView 开发者_运维知识库view = new TextView(context);
view.setText(String.valueOf(position) + ' ' + list.get(position));
return view;
}
}
}
This is the code for my Activity-class:
package org.my.tests;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
public class ListViewTest extends ListActivity {
private static final List<List<String>> lists = new ArrayList<List<String>>(3);
static {
List<String> list = new ArrayList<String>(2);
list.add("a");
list.add("b");
lists.add(list);
list = new ArrayList<String>(2);
list.add("c");
list.add("d");
lists.add(list);
list = new ArrayList<String>(2);
list.add("e");
list.add("f");
lists.add(list);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_test);
setListAdapter(new NestedListAdapter(this, lists));
}
}
The custom ListAdapter used in the actual application is much more complex and uses layout inflation for the nested ListViews. The outcome is the same though.
Does anyone have any ideas on how to fix this to show the two elements in each of the inner Lists?
why you don't use an ExpandableListActivity with their ExpandableListView and ExpandableListAdapter, if you use them, already got the methods: getChild, getGroup and you can build differents View for parents and childs...
精彩评论