【问题标题】:ListView with custom adapter带有自定义适配器的 ListView
【发布时间】:2013-05-21 05:49:52
【问题描述】:

我遵循了几个教程,但我仍然无法填充我的列表视图。 我做错了什么?

这是布局 spaced_list.xml

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

</ListView>

这是 spaced_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" 
    android:background="#efefef">

<TextView
    android:id="@+id/leftItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:gravity="left"
    android:text="Left Text View" />

<TextView
    android:id="@+id/rightItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:text="Right Text View" />

</RelativeLayout>

这就是类

public class AllCategoriesActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spaced_list);


    ListView lv = (ListView) findViewById(R.id.mList);
    TextView abHeader = (TextView) findViewById(R.id.header);
    abHeader.setText("Categories");

    CategoryDataSource cDataSource = new CategoryDataSource(this);
    ArrayList<Category> allCategories = cDataSource.getAllCategories();
    CategoriesAdapter cAdapter = new CategoriesAdapter(this, allCategories);
    lv.setAdapter(cAdapter);

}

public class CategoriesAdapter extends ArrayAdapter<Category>{
    private Context context;
    private ArrayList<Category> categories;

    public CategoriesAdapter(Context context, ArrayList<Category> categories){
        super(context, 0);
        this.context = context;
        this.categories = categories;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.spaced_list_item, parent, false);
        }

        Category c = categories.get(position);  

        TextView tvLeft = (TextView) convertView.findViewById(R.id.leftItem);
        tvLeft.setText(c.getTitle());

        return convertView;
    }
}
}

我确保我从 cDataSource.getAllCategories(); 获取值

【问题讨论】:

  • 你得到什么错误?
  • 您可以查看我在下面给出的答案。此外,您的 spaced_list_item.xml 中有一些不必要的属性,因为它是 RelativeLayout,您可以删除属性 android:orientationandroid:gravity

标签: android android-listview android-adapter


【解决方案1】:

改变这个:

  public CategoriesAdapter(Context context, ArrayList<Category> categories){
        super(context, 0); // <- Wrong constructor
        this.context = context;
        this.categories = categories;
    }

到这里:

  public CategoriesAdapter(Context context, ArrayList<Category> categories){
        super(context, 0, categories);
        this.context = context;
        this.categories = categories;
    }

你目前正在调用超类的this构造函数:

  public ArrayAdapter(Context context, int textViewResourceId) {
        init(context, textViewResourceId, 0, new ArrayList<T>());
    }

这最终会创建一个新的ArrayList 并忽略你传入的数据。

【讨论】:

  • 像魅力一样工作!谢谢。
【解决方案2】:

问题出在适配器的构造函数中。当您致电super() 时,您需要提供您的ArrayList&lt;Category&gt;。代码如下:

public CategoriesAdapter(Context context, ArrayList<Category> categories){
    super(context, 0, categories); //Providing objects to represent in the ListView
    this.context = context;
    this.categories = categories;
}

这是此ArrayAdapter constructor 的参考文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多