java - 自定义ListView不显示任何内容

标签 java android listview custom-adapter

我正在尝试填充 ListView左侧为字符串(食物名称),右侧为项目计数。我做了一个CustomAdapter扩展 BaseAdapter 的类(见下文)在遵循一些教程之后,我可以看到(通过打印语句)至少 getCount()方法被调用,但是我的getView()方法没有被调用,我的 ListView没有出现在我的应用程序上。我究竟做错了什么?我认为这是因为我不知道如何制作 getView()跑到正确的地方。当我使用正常的 ArrayAdapter 时它工作正常带有字符串数组。

以下是我尝试在 MainActivity 类中填充 ListView 的方法:

CustomAdapter customAdapter = new CustomAdapter();
ListView lv1 = (ListView) findViewById(R.id.listView);
lv1.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();

现在,这是我的 CustomAdapter 类:

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

    List<foodItem> mItemList = getDataForListView();

    public CustomAdapter(List<foodItem> list){
        this.mItemList = list;
        // tried this as an alternative
    }

    @Override
    public int getCount() {
        return mItemList.size();
    }

    @Override
    public foodItem getItem(int arg0) {
        return mItemList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup parent) {
        System.out.println("Item list size: " + mItemList.size()); // doesn't show anything in console

        if(arg1==null) {
            LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
            arg1 = inflater.inflate(R.layout.listrow, parent ,false);
// R.layout.listrow is an XML file defined and formatted at 60sp tall
// as that is how tall I want each item in the ListView to be
        }

        TextView foodItem = (TextView)arg1.findViewById(R.id.foodTitle);
        TextView likeCount = (TextView)arg1.findViewById(R.id.likeCount);
        foodItem i = mItemList.get(arg0);
        foodItem.setText(i.getItem());
        likeCount.setText(i.getLikes());

        return arg1;
    }

    public List<foodItem> getDataForListView() {

        List<foodItem> itemList = new ArrayList<foodItem>();
// Test method populating with some data
        for(int i=0; i<50; i++) {
            itemList.add(new foodItem(i, "Item " + i));
        }
        return itemList;
    }
}

我也尝试添加自己的构造函数,但结果是一样的。任何帮助将不胜感激!

编辑:我的主要 Activity 的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.inventoryList.MainActivity" >

    <ListView
        android:id="@+id/foodOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp" />

</RelativeLayout>

最后,这是我的 listrow.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="60sp"
    android:orientation="horizontal"
    android:padding="5sp" >

    <TextView
        android:id="@+id/foodTitle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="12sp"
        android:text="123"
        android:gravity="center" />

    <TextView
        android:id="@+id/likeCount"
        android:layout_width="12sp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="@+string/likeCount"
        android:textSize="10sp"
        tools:ignore="SmallSp" />

</RelativeLayout>

最佳答案

您正在重新声明List<foodItem> itemListgetDataForListView 。这会覆盖成员变量。更改为

itemList = new ArrayList<>();

此外,您应该在构造函数中执行此操作,而不是在成员方法中执行此操作,并在前面添加 m (所以 mItemList )到你的成员变量是很好的风格,有助于防止这个问题。所以大家在一起:

private class CustomAdapter extends BaseAdapter {
    private List<foodItem>  mItemList; //camel case isn't usually applied to classes os this should be FoodItem

    public CustomAdapter() {
        mItemList = new ArrayList<>();
        for (...) {
            ...
        }
    }

    ...
}

更好的是,传递 List给构造函数:

private class CustomAdapter extends BaseAdapter {
    private List<FoodItem> mItemList;

    public CustomAdapter(List<FoodItem> itemList) {
        mItemList = itemList;
    }
    ...
}

关于java - 自定义ListView不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28370891/

相关文章:

java - 使用 swing 创建图像托盘

java - #0001 : java. sql.SQLException : Parameter index out of range (1 > number of parameters, 即 0)

java - 如何使用 jquery 获取带有换行符的 XML?

android - ViewPager 或 ViewFlipper 或其他无限数据集?

android - 如何更改 ListView 上仅选定项目的背景颜色

c++ - 如何从 C++ 设置 ListView 模型?

java - MinecraftServer.getServer 返回 null MinecraftForge

android - React Native Android : Could not get BatchedBridge, 确保你的包被正确打包

java - 安卓 ArrayList IndexOutOfBoundsException

qt - ListView 中没有更新