问题
I followed several tutorials but I still can't populate my list view. What am I doing wrong?
this is the layout 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>
this is 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>
and this is the class
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;
}
}
}
And I made sure that I'm getting values from cDataSource.getAllCategories();
回答1:
Change this:
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;
}
You are currently calling this constructor of the super class:
public ArrayAdapter(Context context, int textViewResourceId) {
init(context, textViewResourceId, 0, new ArrayList<T>());
}
Which will eventually create a new ArrayList
and ignore the data you passed in.
回答2:
The problem is in the constructor in your adapter. You need to provide your ArrayList<Category>
when you call super()
. Here is the code:
public CategoriesAdapter(Context context, ArrayList<Category> categories){
super(context, 0, categories); //Providing objects to represent in the ListView
this.context = context;
this.categories = categories;
}
This is the documentation with the reference for this ArrayAdapter constructor.
来源:https://stackoverflow.com/questions/16755095/listview-with-custom-adapter