如何嵌套列表视图?嵌套、视图、列表

2023-09-07 12:54:17 作者:回眸一笑凌乱了年华

所以这里的问题。我正在写查询一些数据的Web服务的应用程序。返回的数据是字符串的任意数量的

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.

我需要显示这些字符串,这显然是最容易通过在其它的ListView嵌套一个ListView完成的列表。这是使一个ListView的行包含其他列表视图。我碰到的问题是,当我这样做,内部列表视图只显示支持数组的第一个元素。

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.

我使用的延伸BaseAdapter的自定义ListAdapter。使用Eclipse调试我已经能够确定该getView(INT,查看,ViewGroup中)-method每个实例(一个在嵌套ListView中每一行)获取总是叫三次,但始终是与同一INT-参数(也就是0)。次getView()为一个ListAdapter被调用的数目似乎有一点做与背衬数组包含的元素数。

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.

这是我的ListAdapter。一个其他ListAdapter,嵌套InnerListAdapter用于填充内列表

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

这是code为我的活动类:

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

在实际应用中使用的自定义ListAdapter要复杂得多,并且使用布局通货膨胀的嵌套列表视图。其结果是一样的,但。

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?

推荐答案

你为什么不使用ExpandableListActivity他们ExpandableListView和ExpandableListAdapter,如果你使用它们,已经得到了方法:getChild,getGroup,你可以建立型动物查看家长和孩子的...

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...