ListView不加载项目

时间:2017-09-04 16:40:35

标签: android listview

我在片段中创建了一个ListView,并从一个简单的字符串列表中获取结果。

Item.java

public class Item implements Parcelable  {

    public String message;

    public Item(String message) {
        this.message = message;
    }


    protected Item(Parcel in) {
        message = in.readString();
    }

    public static final Creator<Item> CREATOR = new Creator<Item>() {
        @Override
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }

        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(message);
    }
}

item_layout.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="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_text"
            android:textSize="12sp"/>

</LinearLayout>

ListAdapter.java

public class ItemListAdapter extends ArrayAdapter<Item> {

    public ItemListAdapter(Context context, ArrayList<Item> items) {
        super(context, 0, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Item item = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, parent, false);
        }

        TextView messageText = (TextView) convertView.findViewById(R.id.item_text);

        messageText.setText(item.message);

        return convertView;
    }
}

BeveragesItemListFragment.java

public class BeveragesItemListFragment extends ListFragment {

    private ArrayList<Item> array_of_items = new ArrayList<>();;
    private List<String> bevs = Arrays.asList("קולה", "קולה זירו", "דיאט קולה", "נסטי אפרסק");

    public BeveragesItemListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_fragment, container, false);

        TextView title = (TextView) v.findViewById(R.id.list_title);
        title.setText("שתיה קלה");

        ItemListAdapter adapter = new ItemListAdapter(getActivity().getApplicationContext(), array_of_items);

        for (String bev: bevs) {
            Item newItem = new Item(bev);
            adapter.add(newItem);
        }

        return v;
    }
}

应用程序没有崩溃 - 片段加载了它的标题,一切看起来都很顺利。但是,我在ListView中看不到预定义的字符串。也许问题是我在ItemListAdapter初始化中传递的上下文?

我尝试过adapter.notifyDataSetChanged(),但这也没有帮助。

3 个答案:

答案 0 :(得分:2)

您需要在return v;

之前致电setListAdapter(adapter);
   ItemListAdapter adapter = new ItemListAdapter(getActivity().getApplicationContext(), array_of_items);
    setListAdapter(adapter);
//  ^^^^^^^^^^^^^^^^ 
    return v;

并将android:layout_height="match_parent"更改为android:layout_height="wrap_content",以便为其他列表项目腾出空间

答案 1 :(得分:0)

只需通过ListView将适配器设置为ListView.setAdapter即可。适配器本身不会显示任何内容。

答案 2 :(得分:0)

你需要将以下行放在return语句之上。

for (String bev: bevs) {
            Item newItem = new Item(bev);
            array_of_items.add(newItem);
        }

ItemListAdapter adapter = new ItemListAdapter(getActivity(), array_of_items);


    setListAdapter(adapter);