android - 从 ListView 获取列表项
问题描述
当单击该项目时,我有兴趣在ListView 中获取对列表项的对象引用。我的自定义项目 XML 上的每个项目都包含一个hidden
默认情况下的 Button 和一个 TextView。单击某个项目应将其更改visibility
为visible
。
这就是我的意思,在集成图形编辑器中完成。
应该变成这个
这是我getView
对 AdapterView的自定义
@Override
public View getView(int position, View v, ViewGroup viewGroup){
if(v==null){
LayoutInflater li= LayoutInflater.from(getContext());
v= li.inflate(R.layout.custom_list_item, null);
}
Student s= getItem(position);
TextView name= v.findViewById(R.id.txt_name);
TextView surname= v.findViewById(R.id.txt_surname);
TextView id= v.findViewById(R.id.txt_id);
//following methods come from my Student class
if(s != null){
name.setText(s.getName());
surname.setText(s.getSurname());
id.setText(s.getId());
}
return v;
}
这就是我(想要)在我的活动中做的事情,但现在还不行。
ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
getStudents()
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);
tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});
解决方案
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);
tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});
您试图View
在布局中查找 s Activity
,但它们在View
您作为参数获得的 s 中。你可以像这样修复它:
...
TextView tel= view.findViewById(R.id.txt_tel);
Button delete= view.findViewById(R.id.btn_delete);
...
推荐阅读
- odoo-11 - 如何在应用程序菜单上的模块中添加图标?
- android - 将几个字符串值传递给另一个 Activity
- autodesk-forge - 在同一个查看器中加载多个模型时,属性数据库是否正在更新?
- angular - 自定义角度示意图:管道“dasherize”未定义。在角度项目上运行时
- docker - Docker在使用内部网络时不会暴露端口
- dart - Flutter - 通过滑动或按下 floatingActionButton 来展开 bottomNavigationBar
- python - Python C api - 函数重载
- javascript - 表单提交时重定向 webhook
- c# - 使用 KNX.Net 在 Event 之后以编程方式更新图像
- python-2.7 - Python 2.7 import unicode_literals from __future__ 在使用 umauts 读取文件时给出 UnicodeDecodeError