0

我正在尝试创建一个 ListView,其中每个项目都包含一个 TextItem 和一个 ListView(动态长度),但由于某种原因,内部 ListView 中只显示了一个项目。这是不可能的事情还是我做错了什么?

这是我的布局:

<?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">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outerListId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

内部列表项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Profile Name"
        android:id="@+id/nameId"/>
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/innerListId"/>
</RelativeLayout>

代码:

public class ListAllActivity extends Activity {

    protected class MyListItem {
        public String name;
        public List<String> items;

        public MyListItem(String name) {
            this.name = name;
            this.items = items();
        }

        public List<String> items() {
            ArrayList<String> strings = new ArrayList<String>();
            strings.add("item1");
            strings.add("item2");
            return strings;
        }
    }

    public class CustomListViewAdapter extends ArrayAdapter<MyListItem> {
        Context context;

        public CustomListViewAdapter(Context context, int resourceId, List<MyListItem> items) {
            super(context, resourceId, items);
            this.context = context;
        }

        private class ViewHolder {
            TextView nameText;
            ListView myList;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            MyListItem rowItem = getItem(position);

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.profile_exercises_list_item, null);
                holder = new ViewHolder();
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.nameText = (TextView) convertView.findViewById(R.id.profileNameListItemId);
            holder.myList = (ListView) convertView.findViewById(R.id.innerListId);
            List<String> items = rowItem.items();
            holder.nameText.setText(rowItem.name);
            ArrayAdapter<String> myarrayAdapter = new ArrayAdapter<String>(convertView.getContext(), android.R.layout.simple_list_item_1, items);
            holder.myList.setAdapter(myarrayAdapter);
            holder.myList.setTextFilterEnabled(true);

            return convertView;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        ListView listView = (ListView) findViewById(R.id.outerListId);

        ArrayList<MyListItem> allItems = new ArrayList<MyListItem>();
        allItems.add(new MyListItem("name1"));
        allItems.add(new MyListItem("name2"));

        CustomListViewAdapter all = new CustomListViewAdapter(this, android.R.layout.simple_list_item_1, allItems);
        listView.setAdapter(all);
        listView.setTextFilterEnabled(true);
    }
}
4

1 回答 1

1

您可以使用可扩展的 ListView,而不是使用嵌套的 ListView。以下是一些有用的链接,可用于使用可扩展的 ListView。
http://www.dreamincode.net/forums/topic/270612-how-to-get-started-with-expandablelistview/
http://android-adda.blogspot.com/2011/06/custom-expandable-listview。 html

于 2013-07-18T19:16:51.533 回答