具有自定义适配器的ListView

具有自定义适配器的ListView

我遵循了一些教程,但是仍然无法填充列表视图。
我究竟做错了什么?

这是布局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();获取值

最佳答案

更改此:

  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并忽略您传入的数据。

关于android - 具有自定义适配器的ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16755095/

10-08 21:16