android - 如何执行为 Listview 单独创建的 XML 事件
问题描述
我使用 Sqlite 在一个列表视图中显示我的联系人。我分别为列表视图设计了 XML 布局。我通过使用布局充气器在适配器类中使用了该 XML。现在我想为设计的 XML 中的视图执行事件。我怎样才能让这些视图执行事件。
Adapter Class Code:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem;
LayoutInflater inflator=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listItem=inflator.inflate(R.layout.activity_contacts_design,parent,false);
contacts=new ArrayList<>();
final ContactsModel contact = contacts.get(position);
ImageView image = (ImageView)listItem.findViewById(R.id.photo_iv);
image.setImageBitmap(contact.getImage());
TextView name = (TextView) listItem.findViewById(R.id.name_tv);
name.setText(contact.getName());
TextView number = (TextView) listItem.findViewById(R.id.number_tv);
number.setText(contact.getNumber());
ImageView img=(ImageView)listItem.findViewById(R.id.edit_iv);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db=new DatabaseHelper(mContext);
int id= contact.getId();
ContactsModel contact = db.getContact(id);
}
});
return listItem;
}
为我的列表视图观察下图,单独的 XML 设计用于 kist 视图和图像以执行事件
请帮我。
getContact()
方法代码:
public ContactsModel getContact(long id){
ContactsModel c=new ContactsModel();
SQLiteDatabase db=getReadableDatabase();
Cursor cursor=db.query(c.Table_Name,new String[]{c.ColumnContactName,c.ColumnNumber,c.ColumnPhoto},c.Columnid +"=?",
new String[]{String.valueOf(id)},null,null,null,null);
if(cursor!=null){
cursor.moveToFirst();
}
byte[] bytes = Base64.decode(cursor.getString(cursor.getColumnIndex(ContactsModel.ColumnPhoto)), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ContactsModel model=new ContactsModel(
cursor.getString(cursor.getColumnIndex(ContactsModel.ColumnContactName)),
cursor.getString(cursor.getColumnIndex(ContactsModel.ColumnNumber)),bitmap);
cursor.close();
return model;
}
解决方案
您必须id
在 xml 文件中提供这两个图标。
然后通过 id in 获取这两个图标adapter class
。
在这两个图标上设置点击侦听器并执行您想要的任何操作。
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItem;
LayoutInflater inflator=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listItem=inflator.inflate(R.layout.activity_contacts_design,parent,false);
ContactsModel contact = contacts.get(position);
ImageView image = (ImageView)listItem.findViewById(R.id.photo_iv);
image.setImageBitmap(contact.getImage());
//Give icons a id in xml file and get those view in adapter class and set onclick listener to perform event.
ImageView icon_one = (ImageView)listItem.findViewById(R.id.icon_one);
ImageView icon_two = (ImageView)listItem.findViewById(R.id.icon_two);
icon_one.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
***Do what you want with the click here***
}
});
icon_two.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
***Do what you want with the click here***
}
});
TextView name = (TextView) listItem.findViewById(R.id.name_tv);
name.setText(contact.getName());
TextView number = (TextView) listItem.findViewById(R.id.number_tv);
number.setText(contact.getNumber());
return listItem;
}
推荐阅读
- node.js - 如何在 Winston 中为自定义日志格式添加额外参数?
- excel - 当行的A列对所有行都是Yes时锁定行中的选定单元格
- json - 将MongoDB查询结果导出到文件
- javascript - 我可以检测从书签工作的 javascript 代码吗?
- ssl - 您不是此证书的域的授权所有者
- c - 将所有位设置为最低有效位
- google-app-engine - 静态文件被解释为具有不同 MIME 类型的文档
- r - 防止 ggplot2 降低 x 轴水平
- angular-cli - Angular 7.2 - ng new project 命令在我的根目录中创建了一堆 node_modules
- c++ - 如何修复井字游戏中的随机数生成器