Android - 为什么ListView里面的ListView不起作用?

时间:2016-07-04 15:26:24

标签: android listview

我的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:id="@+id/address" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=""
        android:layout_below="@+id/address"
        android:id="@+id/win_title" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/win_list"
        android:layout_below="@+id/win_title"
        android:layout_weight="1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=""
        android:id="@+id/lose_title" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lose_list"
        android:layout_weight="1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=""
        android:id="@+id/cannot_compare_title" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cannot_compare_list"
        android:layout_weight="1" />

</LinearLayout>

这是我的自定义适配器:

public class AnalysisResultAdapter extends ArrayAdapter<ComparisonResult> {

    static class ViewHolder {
        TextView address;

        TextView win_title;
        ListView win_list;

        TextView lose_title;
        ListView lose_list;

        TextView cannot_compare_title;
        ListView cannot_compare_list;
    }

    private Hashtable<String, SevenEleven[]> sevenElevenData;

    public AnalysisResultAdapter(Context context, int resource, List<ComparisonResult> houses) {
        super(context, resource, houses);
    }

    public void setSevenElevenData(Hashtable<String, SevenEleven[]> sevenElevenData) {
        this.sevenElevenData = sevenElevenData;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;

        if (view == null) {
            LayoutInflater viewInflater;
            viewInflater = LayoutInflater.from(getContext());
            view = viewInflater.inflate(R.layout.analysis_result_list_item, null);

            holder = new ViewHolder();
            holder.address = (TextView) view.findViewById(R.id.address);

            holder.win_title = (TextView) view.findViewById(R.id.win_title);
            holder.win_list = (ListView) view.findViewById(R.id.win_list);

            holder.lose_title = (TextView) view.findViewById(R.id.lose_title);
            holder.lose_list = (ListView) view.findViewById(R.id.lose_list);

            holder.cannot_compare_title = (TextView) view.findViewById(R.id.cannot_compare_title);
            holder.cannot_compare_list = (ListView) view.findViewById(R.id.cannot_compare_list);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (this.sevenElevenData.size() == 0) {
            return view;
        }

        ComparisonResult result = getItem(position);
        holder.address.setText(result.house.Address);

        if (result.win.size() > 0) {
            ArrayList<String> storeNames = new ArrayList<String>();
            for (int storeIndex : result.win) {
                storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
            }

            holder.win_title.setText("win");
            holder.win_list.setAdapter(new ArrayAdapter<String>(
                getContext(),
                R.layout.text_list,
                R.id.text_list,
                storeNames
            ));
        }

        if (result.lose.size() > 0) {
            ArrayList<String> storeNames = new ArrayList<String>();
            for (int storeIndex : result.lose) {
                storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
            }

            holder.lose_title.setText("lose");
            holder.lose_list.setAdapter(new ArrayAdapter<String>(
                    getContext(),
                    R.layout.text_list,
                    R.id.text_list,
                    storeNames
            ));
        }

        if (result.cannotBeCompared.size() > 0) {
            ArrayList<String> storeNames = new ArrayList<String>();
            for (int storeIndex : result.cannotBeCompared) {
                storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
            }

            holder.cannot_compare_title.setText("cannotBeCompared");
            holder.cannot_compare_list.setAdapter(new ArrayAdapter<String>(
                    getContext(),
                    R.layout.text_list,
                    R.id.text_list,
                    storeNames
            ));
        }


        return view;
    }
}

问题是:只有@ + id / address TextView成功显示文本,其他内容未显示。但我不知道为什么会这样。

我该如何解决这个问题?有人能帮助我吗?

感谢。

2 个答案:

答案 0 :(得分:1)

使用 ScrollView intead of external(parent)ListView。

并使用LisView作为内部ListView。

它为您提供滚动功能。

答案 1 :(得分:0)

1 / layout_below仅适用于RelativeLayout,您使用的是具有水平方向的LinearLayout,因此所有元素将并排呈现,并且所有元素都具有layout_width:&#34; match_parent&#34;所以只会显示第一个元素。

2 /当你在另一个scrollView中使用ListView时,渲染器无法计算第二个的高度