【发布时间】:2019-12-31 10:42:05
【问题描述】:
我使用 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;
}
【问题讨论】:
-
你能显示你的适配器代码吗?
-
@TakeInfos,我添加了代码,请检查一次
-
@Priyanka 你能分享 XML 设计吗?
-
@Priyanka 我已经添加了代码检查它。