首页 > 解决方案 > 如何执行为 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;
}

标签: androidxmllistview

解决方案


您必须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;
}


推荐阅读