我遵循了几个教程,但仍然无法填充我的列表视图。
我究竟做错了什么?
这是布局 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;
}
To this:
public CategoriesAdapter(Context context, ArrayList<Category> categories){
super(context, 0, categories);
this.context = context;
this.categories = categories;
}
您当前正在拨打电话this https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java#L103-L105超类的构造函数:
public ArrayAdapter(Context context, int textViewResourceId) {
init(context, textViewResourceId, 0, new ArrayList<T>());
}
这最终将创建一个新的ArrayList
并忽略您传入的数据。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)