我正在創建一個cutomized listView。 listView中每個項目的模型如「model_view」佈局所示。 我將數據設置爲適配器,如下面的「設置適配器」所示。 的cutomized適配器如圖「CutomizedAdapter」listview項不顯示
當我運行的應用程序,也有出現在屏幕
爲什麼列表視圖的項目沒有顯示
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;
}
}
現在應用程序崩潰,因爲「打氣筒」未初始化......我試圖按如下方式初始化:吹氣=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);但「getSystemService」不能被識別 – user2121
在你的構造函數中它應該是這樣的'inflater =(LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);' –
更新了我的答案! –