我提前为下面的格式道歉。我还没有在这个网站上发布很多内容。我在绑定视图的第148行得到一个空指针异常,特别是在以下行:
AdditionalInfoView additionalInfoView = (AdditionalInfoView) view.getTag();
additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
我确信布局有一个带有id标题的文本字段,并且数据库在标题列中有数据。我不确定这个方法是否在所有方面都有效,并且当问题确实发生时,它只是随机给我一个错误。
我试图实现一个可以处理多种类型数据的列表视图,这样我就可以用我想要的数据动态创建页面。我这样做了吗?有更好的实施吗?或者这根本不推荐?以下适配器的完整代码:
一个注意事项 - 我看到一些实现具有listadapter正在显示的不同类型布局的计数。我需要在某处设置吗?
修改 我已经在isNull()中包含了对游标的所有调用,以确保它不是没有工作的游标。我仍然收到错误,所以我认为某种方式某些内容肯定是错误的。
/** ResourcesAdapter
*
* This takes in a cursor for the resources table and attaches the
* appropriate view to the appropriate data field. Fields are:
* AdditionalInfo
* Address
* AppLink
* Paragraph
* Phone
* Space
* TextWithBullets
* TitleBreak
* WebLink
*
* TODO- I'm totally skipping AppLink for later.
*/
public class ResourcesAdapter extends CursorAdapter {
public ResourcesAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
static class PhoneView {
TextView title;
TextView phone;
}
static class TitleBreakView {
TextView title;
}
static class AdditionalInfoView {
TextView title;
}
static class AddressView {
TextView title;
TextView address;
}
static class AppLinkView {
TextView title;
}
static class ParagraphView {
TextView paragraph;
}
static class TextWithBulletsView {
TextView text;
TextView bullet1;
TextView bullet2;
TextView bullet3;
TextView bullet4;
TextView bullet5;
}
static class WebLinkView {
TextView title;
}
/** newView
*
* This method gets the views that we later bind the data to. Supposedly this will recycle views
* too, but it's not the convertView method I'm used to from array adapters. If it's happening,
* its happening in the background.
**/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = null;
if (cursor.getString(cursor.getColumnIndex("type")).equals("Phone")) {
view = LayoutInflater.from(context).inflate(R.layout.phone, parent, false);
PhoneView phoneView = new PhoneView();
phoneView.title = (TextView)(view.findViewById(R.id.title));
phoneView.phone = (TextView) (view.findViewById(R.id.phone));
view.setTag(phoneView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("TitleBreak")) {
view = LayoutInflater.from(context).inflate(R.layout.title_break, parent, false);
TitleBreakView titleBreakView = new TitleBreakView();
titleBreakView.title = (TextView)(view.findViewById(R.id.title));
view.setTag(titleBreakView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("AdditionalInfo")) {
view = LayoutInflater.from(context).inflate(R.layout.additional_info, parent, false);
AdditionalInfoView additionalInfoView = new AdditionalInfoView();
additionalInfoView.title = (TextView)(view.findViewById(R.id.title));
view.setTag(additionalInfoView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Address")) {
view = LayoutInflater.from(context).inflate(R.layout.address, parent, false);
AddressView addressView = new AddressView();
addressView.title = (TextView)(view.findViewById(R.id.title));
addressView.address = (TextView)(view.findViewById(R.id.address));
view.setTag(addressView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("AppLink")) {
view = LayoutInflater.from(context).inflate(R.layout.app_link, parent, false);
AppLinkView appLinkView = new AppLinkView();
appLinkView.title = (TextView)(view.findViewById(R.id.title));
view.setTag(appLinkView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Paragraph")) {
view = LayoutInflater.from(context).inflate(R.layout.paragraph, parent, false);
ParagraphView paragraphView = new ParagraphView();
paragraphView.paragraph = (TextView)(view.findViewById(R.id.paragraph));
view.setTag(paragraphView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Space")) {
view = LayoutInflater.from(context).inflate(R.layout.space, parent, false);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("TextWithBullets")) {
view = LayoutInflater.from(context).inflate(R.layout.text_with_bullets, parent, false);
TextWithBulletsView textWithBulletsView = new TextWithBulletsView();
textWithBulletsView.text = (TextView)(view.findViewById(R.id.text));
textWithBulletsView.bullet1 = (TextView)(view.findViewById(R.id.bullet1));
textWithBulletsView.bullet2 = (TextView)(view.findViewById(R.id.bullet2));
textWithBulletsView.bullet3 = (TextView)(view.findViewById(R.id.bullet3));
textWithBulletsView.bullet4 = (TextView)(view.findViewById(R.id.bullet4));
textWithBulletsView.bullet5 = (TextView)(view.findViewById(R.id.bullet5));
view.setTag(textWithBulletsView);
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("WebLink")) {
view = LayoutInflater.from(context).inflate(R.layout.web_link, parent, false);
WebLinkView webLinkView = new WebLinkView();
webLinkView.title = (TextView)(view.findViewById(R.id.title));
view.setTag(webLinkView);
}
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (cursor.getString(cursor.getColumnIndex("type")).equals("Phone")) {
PhoneView phoneView = (PhoneView) view.getTag();
phoneView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
phoneView.phone.setText(cursor.getString(cursor.getColumnIndex("data1")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("TitleBreak")) {
TitleBreakView titleBreakView = (TitleBreakView) view.getTag();
titleBreakView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("AdditionalInfo")) {
AdditionalInfoView additionalInfoView = (AdditionalInfoView) view.getTag();
additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Address")) {
AddressView addressView = (AddressView) view.getTag();
addressView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
addressView.address.setText(cursor.getString(cursor.getColumnIndex("data1")));
//TODO-Properly format the address
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("AppLink")) {
AppLinkView appLinkView = (AppLinkView) view.getTag();
appLinkView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Paragraph")) {
ParagraphView paragraphView = (ParagraphView) view.getTag();
paragraphView.paragraph.setText(cursor.getString(cursor.getColumnIndex("data1")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("Space")) {
//Does something need to go here? It's just a spacer!
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("TextWithBullets")) {
TextWithBulletsView textWithBulletsView = (TextWithBulletsView) view.getTag();
textWithBulletsView.text.setText(cursor.getString(cursor.getColumnIndex("title")));
textWithBulletsView.bullet1.setText(cursor.getString(cursor.getColumnIndex("data1")));
textWithBulletsView.bullet2.setText(cursor.getString(cursor.getColumnIndex("data2")));
textWithBulletsView.bullet3.setText(cursor.getString(cursor.getColumnIndex("data3")));
textWithBulletsView.bullet4.setText(cursor.getString(cursor.getColumnIndex("data4")));
textWithBulletsView.bullet5.setText(cursor.getString(cursor.getColumnIndex("data5")));
} else if (cursor.getString(cursor.getColumnIndex("type")).equals("WebLink")) {
WebLinkView webLinkView = (WebLinkView) view.getTag();
webLinkView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
}
}
}
答案 0 :(得分:0)
我得到了它的工作。上述实现完全正确。问题是,因为我使用名称R.id.title来使所有这些不同的布局保持一致,这掩盖了我忘了将R.id.title TextView添加到该特定布局。我以为我先检查了一下,但有很多不同的布局,我认为我看错了。以下是我检查以确保问题不在光标上的方法:
if (!cursor.isNull(cursor.getColumnIndex("title"))) {
additionalInfoView.title.setText(cursor.getString(cursor.getColumnIndex("title")));
}