我有一个适配器,它显示了一个 listView 的两个视图——一个 checktextview 和一个普通的 textview。我想在用户不在时使用checkedtextview,MarkMode
以便他/她可以标记项目,并且我想在用户不在时使用textview MarkMode
。
所以这是我的原始代码,它只有MarkMode
.
public class ContactsAdapter extends BaseAdapter {
private List<Contact> mData;
private LayoutInflater mInflater;
private Context context;
public ContactsAdapter(List<Contact> mData, Context context) {
this.mData = mData;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
switch (context.getSharedPreferences("settings",
HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
case 1:
return mData.get(position).getLastName() + " "
+ mData.get(position).getFirstName();
case 2:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getMobileNumber();
case 3:
return mData.get(position).getLastName() + " "
+ mData.get(position).getMobileNumber();
default:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getLastName();
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView result;
if (convertView == null) {
result = (CheckedTextView) mInflater.inflate(
R.layout.home_screen_contacts_mark_view, parent, false);
} else {
result = (CheckedTextView) convertView;
}
final String contactRow = getItem(position);
result.setText(contactRow);
result.setBackgroundResource(R.drawable.list_selector);
return result;
}
}
现在我把它改成了这个,所以我可以改变一个名为的类变量isMarkMode
,这样它就可以显示不同的视图......
public class ContactsAdapter extends BaseAdapter {
private List<Contact> mData;
private LayoutInflater mInflater;
private Context context;
public static boolean inMarkMode = false;
public ContactsAdapter(List<Contact> mData, Context context) {
this.mData = mData;
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
switch (context.getSharedPreferences("settings",
HomeScreenActivity.MODE_PRIVATE).getInt("displayChoice", -1)) {
case 1:
return mData.get(position).getLastName() + " "
+ mData.get(position).getFirstName();
case 2:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getMobileNumber();
case 3:
return mData.get(position).getLastName() + " "
+ mData.get(position).getMobileNumber();
default:
return mData.get(position).getFirstName() + " "
+ mData.get(position).getLastName();
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CheckedTextView result = null;
TextView normalResult = null;
if (inMarkMode) {
if (convertView == null) {
result = (CheckedTextView) mInflater.inflate(
R.layout.home_screen_contacts_mark_view, parent, false);
} else {
result = (CheckedTextView) convertView;
}
} else {
if (convertView == null) {
normalResult = (TextView) mInflater.inflate(
R.layout.home_screen_contacts_view, parent, false);
} else {
normalResult = (TextView) convertView;
}
}
final String contactRow = getItem(position);
result.setText(contactRow);
result.setBackgroundResource(R.drawable.list_selector);
normalResult.setText(contactRow);
normalResult.setBackgroundResource(R.drawable.list_selector);
if (inMarkMode) {
return result;
} else {
return normalResult;
}
}
}
}
最初它看起来像是在工作,但后来它停止了,我收到一个 logcat 错误,说NullPointerException
这一行有一个:result.setText(contactRow);
我怎样才能解决这个问题?任何帮助,将不胜感激。