我正在创建一个cutomized listView。 listView中每个项目的模型如“model_view”布局所示。 我将数据设置为适配器,如下面的“set adapter”所示。 cutomized适配器如“CutomizedAdapter”
所示当我运行应用程序时,屏幕上没有出现任何项目
为什么列表视图的项目没有显示
model_view :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="Name"/>
<TextView
android:id="@+id/tvAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="Address: "/>
<TextView
android:id="@+id/tvGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="gender: "/>
</LinearLayout>
设置适配器:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mListView = (ListView) findViewById(R.id.listview);
this.mModelList = new ArrayList<RowModel>();
this.mModelList.add(new RowModel("xx", "Mümchen", "M"));
this.mModelList.add(new RowModel("xx", "Karlsruhe", "M"));
this.mModelList.add(new RowModel("xx", "Mümchen", "F"));
this.mModelList.add(new RowModel("xx", "Karlsruhe", "F"));
this.mModelList.add(new RowModel("xx", "Alexandria", "M"));
this.mModelList.add(new RowModel("xx", "Cairo", "F"));
CustomizedAdapter adapter = new CustomizedAdapter(this, R.layout.model_view, this.mModelList);
this.mListView.setAdapter(adapter);
CutomizedAdapter :
public class CustomizedAdapter extends BaseAdapter {
private Context mCxt = null;
private int mRowModelLayout;
private ArrayList<RowModel> mRowsList = null;
private static LayoutInflater inflater=null;
public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) {
this.mCxt = cxt;
this.mRowModelLayout = rowModelLayout;
this.mRowsList = rowsList;
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder{
public TextView tvName;
public TextView tvAddress;
public TextView tvGender;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final int pos = position;
ViewHolder holder = new ViewHolder();
View rowView;
convertView = inflater.inflate(R.layout.model_view, null);
holder.tvName=(TextView) convertView.findViewById(R.id.tvName);
holder.tvAddress=(TextView) convertView.findViewById(R.id.tvAddress);
holder.tvGender=(TextView) convertView.findViewById(R.id.tvGender);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mCxt, "You Clicked "+mRowsList.get(pos).getName(), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
答案 0 :(得分:5)
为什么你不填写以下方法?
@Override
public int getCount() {
return mRowsList.size();
}
@Override
public Object getItem(int position) {
return mRowsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
此外,您尚未初始化inflater
所以,在你的构造函数中初始化它,如
public CustomizedAdapter(Context cxt, int rowModelLayout, ArrayList<RowModel> rowsList) {
this.mCxt = cxt;
this.mRowModelLayout = rowModelLayout;
this.mRowsList = rowsList;
inflater = (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
答案 1 :(得分:1)
因为你在这些方法中返回了错误的东西:
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
这些函数不会返回任何内容,请将其更改为:
@Override
public int getCount() {
return mRowList.size();
}
@Override
public Object getItem(int position) {
return mRowList.get(position);
}
@Override
public long getItemId(int position) {
return position; //or mRowList.get(position).getID()
}