我正在尝试将自定义ListView
放入Dialog
。我的代码如下:
dialog = new Dialog(MyWallet.this);
dialog.setContentView(R.layout.emergency_contacts);
dialog.setTitle("Emergency Contacts");
dialog.show();
mAdapter.addInfo("Name");
ListView lv = (ListView) dialog.findViewById(android.R.id.list);
if (lv != null) {
lv.setAdapter(mAdapter);
}
然后我的自定义Adapter
:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_INFO = 1;
private static final int TYPE_MAX_COUNT = 1;
private LayoutInflater mInflater;
private int noteFull = 0;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addInfo(final String info) {
mInfo.add(info);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mInfo.size();
}
@Override
public String getItem(int position) {
return mInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return TYPE_INFO;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("getView " + position + " " + convertView);
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_INFO:
convertView = mInflater.inflate(R.layout.emergency_contacts_list, null);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.name.setText(mInfo.get(position));
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
switch(type) {
case TYPE_INFO:
convertView = mInflater.inflate(R.layout.emergency_contacts_list, null);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.name.setText(mInfo.get(position));
break;
}
}
return convertView;
}
}
public static class ViewHolder {
public TextView name;
public TextView phoneNumber;
public TextView emailAddress;
}
代码崩溃(<{1}}位于
holder.name =(TextView)convertView.findViewById(R.id.name);
我猜是因为NullPointerException
不是正确的观点。如何将其设置为右convertView
?
修改:View
:
Code for emergency_contacts_list
编辑2:完整<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left">
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Name">
</TextView>
<TextView
android:id="@+id/phone"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Phone Number">
</TextView>
<TextView
android:id="@+id/email"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="left"
android:text="Email Address">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:gravity="right|center_vertical"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/call"
android:layout_height="35px"
android:layout_width="50px"
android:background="@drawable/custom_button"
android:layout_margin="3px"
android:text="Call">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
:
logcat
答案 0 :(得分:1)
显示NullPointerException
,因为当convertView
方法中的getView()
非空(已回收)而非使用时,您会为新{{1} } 到位。 View
方法应为:
getView()
答案 1 :(得分:0)
在MyCustomAdapter中定义你的静态类。这对你有用。例如
public class MyCustomAdapter extends BaseAdapter {
public static class ViewHolder {
public TextView name;
public TextView phoneNumber;
public TextView emailAddress;
}
}
答案 2 :(得分:0)
以下是您应如何进行布局填充的示例代码
LayoutInflater li = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View vDialogTextSize = li.inflate(R.layout.options_dialog_textsize, (ViewGroup) findViewById(R.id.Custom_Size_Dialog));
所有信息均来自此网站Dialogs.