[英]Android CustomAdapter for a listView in a Dialog
嗨,我想使用自定義ArrayList適配器在對話框中創建ListView,以便當我單擊該項目以檢索Object時。
我所做的是以下操作:為對話框創建帶有ListView元素的布局
<?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"
android:orientation="vertical" >
<ListView
android:id="@+id/dialogListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
為listView中的項目創建了一個自定義布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/singleItemDialogList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:background="#336699" />
</LinearLayout>
我有我的自定義適配器
public class AddressBookAdapter extends BaseAdapter {
private List<AddressBook> objects;
private LayoutInflater layoutInflater;
public AddressBookAdapter(Context context, List<AddressBook> objects) {
this.objects = objects;
layoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.dialog_list_layout, null);
holder = new ViewHolder();
holder.simpleItemDialog = (TextView) convertView.findViewById(R.id.singleItemDialogList);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.simpleItemDialog.setText(objects.get(position).getAddress());
return convertView;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public AddressBook getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
static class ViewHolder {
TextView simpleItemDialog;
}
}
最后,我在其中創建對話框並設置適配器
final Dialog dialog = new Dialog(this);
View view = getLayoutInflater().inflate(R.layout.dialog_list_layout, null);
ListView lv = (ListView) view.findViewById(R.id.dialogListView);
AddressBookAdapter aba = new AddressBookAdapter(Logged.this, cachedData.getAddressBookList());
Log.w("ABA",""+aba.getCount());
lv.setAdapter(aba);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.w("CLICK","ITEM");
}
});
dialog.setContentView(view);
dialog.show();
現在,當我打開對話框時,我得到以下錯誤:NullPointerException並指向AddressBookAdapter.getView(第38行)->第38行是: holder.simpleItemDialog.setText(objects.get(position).getAddress());
謝謝
調試並檢查您的列表是否為空或包含任何值。您的列表可能為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.