为什么我的Android ListAdapter只显示一项?

克拉默65

在我的Android应用中,我正在尝试构建我的第一个自定义listadapter。我现在设法使它显示一项,但不幸的是,仅当列表中有8条时才显示一项。我的适配器如下所示:

public class PossibilitiesAdapter extends BaseAdapter {
    private AdapterCallbacks callbacks;
    private List<String> possibilitiesList = new ArrayList<String>();
    private LayoutInflater inflater;

    public PossibilitiesAdapter(Context context) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setPossibilitiesList(List<String> possibilitiesList) {
        for (String possibility : possibilitiesList) {
            addItem(possibility);
        }
    }

    public void addItem (final String item) {
        possibilitiesList.add(item);
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView possibilityTitle;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_posibility, viewGroup, false);
        holder.possibilityTitle = (TextView) convertView.findViewById(R.id.text_possibility);
        holder.possibilityTitle.setText(possibilitiesList.get(position));

        return convertView;
    }

    @Override
    public int getCount() {
        return possibilitiesList.size();
    }
    // And here are some more things like getItemId() and getItem()
}

我从片段中设置了可能性列表,如下所示:

List<String> list = Arrays.asList(getResources().getStringArray(R.array.the_possibilities));
Log.e(this, new Integer(list.size()).toString()); // Outputs 8, so there are enough in the array.
adapter.setPossibilitiesList(list);

我的fragment_possibilities.xml看起来像这样:

<ListView
    android:id="@+id/list_possibilities"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

和我的list_item_possibility.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_possibility"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

因此,问题在于它仅显示第一项,仅此而已。有谁知道问题是什么以及如何解决?

西蒙·舒伯特

您是否将列表嵌套在ScrollView中?通常只会显示一项。尝试避免这种情况o使用以下方法。

/**
 * read all adapter views and calculate total length
 *
 * @param listView
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    setListViewHeight(listView, totalHeight);
}

/**
 * set listview height
 *
 * @param listView
 * @param height
 */
public static void setListViewHeight(ListView listView, int height) {
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = height + (listView.getDividerHeight() * (listView.getAdapter().getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

不,那是行不通的,正如我所说,您不能将ListView放在ScrollView中。

尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
    android:id="@+id/list_possibilities"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档