public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View vi= convertView;
if(convertView==null){
viewHolder = new ViewHolder();
vi = inflater.inflate(R.layout.list_row, null);
viewHolder.content = (TextView)vi.findViewById(R.id.f_content);
viewHolder.cat_id = (TextView)vi.findViewById(R.id.c_id);
viewHolder.fact_id = (TextView)vi.findViewById(R.id.f_id);
vi.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> fact = new HashMap<String, String>();
fact = data.get(position);
// Setting all values in listview
viewHolder.fact_id.setText(fact.get("_id"));
viewHolder.cat_id.setText(fact.get("cat_id"));
viewHolder.content.setText(fact.get("fact_content"));
return vi;
}
我创建的LazyAdapter
扩展了BaseAdaper
以与ListView
一起使用。
此适配器在ListView中填充数据。当我点击前4个可见列表项时,它会打印该项的值。 问题是当我向下滚动列表视图并单击第5项时。应用程序崩溃说空指针异常。日志跟踪在此处供参考。
05-13 23:10:43.943:E / AndroidRuntime(3360):致命异常:主要 05-13 23:10:43.943:E / AndroidRuntime(3360):java.lang.NullPointerException 05-13 23:10:43.943:E / AndroidRuntime(3360):at com.example.facts.MainActivity $ 3.onItemClick(MainActivity.java:100) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.widget.AdapterView.performItemClick(AdapterView.java:284) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.widget.ListView.performItemClick(ListView.java:3569) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1831) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.os.Handler.handleCallback(Handler.java:587) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.os.Handler.dispatchMessage(Handler.java:92) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.os.Looper.loop(Looper.java:150) 05-13 23:10:43.943:E / AndroidRuntime(3360):在android.app.ActivityThread.main(ActivityThread.java:4389) 05-13 23:10:43.943:E / AndroidRuntime(3360):at java.lang.reflect.Method.invokeNative(Native Method) 05-13 23:10:43.943:E / AndroidRuntime(3360):at java.lang.reflect.Method.invoke(Method.java:507) 05-13 23:10:43.943:E / AndroidRuntime(3360):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849) 05-13 23:10:43.943:E / AndroidRuntime(3360):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 05-13 23:10:43.943:E / AndroidRuntime(3360):at dalvik.system.NativeStart.main(Native Method)
Onclick方法是这样的:
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
RelativeLayout node = (RelativeLayout)parent.getChildAt(position);
TextView fact = (TextView) node.findViewById(R.id.c_id);
Log.d("ListItem Clicked", "id: " + id + " position: "
+ position + " artist " + fact.getText());
}
});
答案 0 :(得分:2)
我的建议是重新审核您的onItemClickListener
实施。
根据您的目的,您根本不需要访问父级。试试这个。
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView fact = (TextView) view.findViewById(R.id.c_id);
Log.d("ListItem Clicked", "id: " + id + " position: "
+ position + " artist " + fact.getText());
}
});
方法中提供的view参数实际上是单击的视图。
图; AdapterView中被单击的视图(这将是适配器提供的视图)
有关详细信息,请参阅AdapterView.OnItemClickListener。
家长可以帮助您确定正在采取行动的AdapterView
。例如,假设您有两个ListAdapter
但只想使用一个侦听器,那么它将帮助您指定哪个是侦听器。