【问题标题】:How to perform a event of XML which is created for Listview separately如何执行为 Listview 单独创建的 XML 事件
【发布时间】: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 我已经添加了代码检查它。

标签: android xml listview


【解决方案1】:

您必须将id 赋予xml 文件中的这两个图标。

然后通过adapter class 中的id 获取这两个图标。

在这两个图标上设置点击监听器并执行任何你想要的操作。

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;
}

【讨论】:

【解决方案2】:

您可以按照下面的代码:

class ContactAdapter(
    private val onClickListener: OnClickListener
) : ListAdapter<ContactsModel, RecyclerView.ViewHolder>(DiffCallback) {

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ...
        listItem.setOnClickListener {
            onClickListener.onClick(item)
        }
    }


    class OnClickListener(val clickListener: (item: ContactsModel) -> Unit) {
        fun onClick(item: ContactsModel) = clickListener(item)
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2017-06-29
    • 2018-01-08
    • 2010-09-16
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多