用于对话框中listView的Android自定义适配器

时间:2014-01-09 01:16:41

标签: java android android-listview android-dialog android-adapter

您好我想在Dialog中创建一个ListView,使用自定义ArrayList适配器,这样当我点击该项目来检索Object时。

我所做的是以下内容: 使用Dialog的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();

好的,现在当我打开Dialog时,我收到错误:NullPointerException并指向AddressBookAdapter.getView(第38行) - &gt;第38行是:holder.simpleItemDialog.setText(objects.get(position).getAddress());

谢谢

1 个答案:

答案 0 :(得分:0)

调试并检查列表是否为空或包含任何值。您的列表可能为空。