简体   繁体   English

对话框中的listView的Android CustomAdapter

[英]Android CustomAdapter for a listView in a Dialog

Hi i want to make a ListView in a Dialog, with a custom ArrayList adapter, so that when i click on the item to retrieve the Object. 嗨,我想使用自定义ArrayList适配器在对话框中创建ListView,以便当我单击该项目以检索Object时。

What i have done is the follow: Create a Layout with a ListView element for the 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>

Created a Custom Layout for items in the listView 为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>

I have my Custom adapter 我有我的自定义适配器

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;
}
}

And finally where i create the dialog and set the adapter 最后,我在其中创建对话框并设置适配器

        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();

Ok now when i open the Dialog i get the error: NullPointerException and points to AddressBookAdapter.getView(line 38) -> Line 38 is: holder.simpleItemDialog.setText(objects.get(position).getAddress()); 现在,当我打开对话框时,我得到以下错误:NullPointerException并指向AddressBookAdapter.getView(第38行)->第38行是: holder.simpleItemDialog.setText(objects.get(position).getAddress());

Thank you 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM