gpt4 book ai didi

java - 滚动时出现惰性列表适配器问题

转载 作者:行者123 更新时间:2023-12-02 05:50:02 25 4
gpt4 key购买 nike

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 个可见列表项时,它会打印该项目的值。问题是当我向下滚动 ListView 并单击第 5 项时。应用程序崩溃并提示空指针异常。日志跟踪在这里供引用。

05-13 23:10:43.943: E/AndroidRuntime(3360): FATAL EXCEPTION: main 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): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.ListView.performItemClick(ListView.java:3569) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.handleCallback(Handler.java:587) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Handler.dispatchMessage(Handler.java:92) 05-13 23:10:43.943: E/AndroidRuntime(3360): at android.os.Looper.loop(Looper.java:150) 05-13 23:10:43.943: E/AndroidRuntime(3360): at 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());
}
});

最佳答案

我的建议是重新访问您的 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 参数实际上是正在单击的 View 。

view; The view within the AdapterView that was clicked (this will be a view provided by the adapter)

请引用AdapterView.OnItemClickListener了解更多详情。

父级可以帮助您识别正在执行操作的 AdapterView。例如,假设您有两个 ListAdapter 但只想使用一个监听器,那么它将帮助您指定哪个监听器。

关于java - 滚动时出现惰性列表适配器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com