gpt4 book ai didi

android - 将参数传递给 ListView 行中按钮上的 onclick 监听器

转载 作者:IT老高 更新时间:2023-10-28 23:08:24 26 4
gpt4 key购买 nike

我刚启动android,遇到了一些问题。
我创建了一个从数据库填充的 ListView
每行都有一个按钮,用于从列表和数据库中删除项目。

我能够将事件监听器连接到按钮,但我无法确定要删除的匹配数据库记录。

我的类(class)如下所示

public class FeedArrayAdapter extends ArrayAdapter<Feed> implements OnClickListener
{
private ARssEReaderDBAdapter dba;
private String TAG = "FeedArrayAdapter";

public FeedArrayAdapter(Context context, int textViewResourceId, List<Feed> items) {
super(context, textViewResourceId, items);
Log.w(TAG, "List");


@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.w(TAG, "getView");
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feedlistrow, null);
}
Feed feed = getItem(position);
if (feed != null) {
TextView title = (TextView)v.findViewById(R.id.TextView01);
if (title != null) {
title.setText(feed.getTitle());
}
Button btnDelete = (Button)v.findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(this); //btnDeleteFeedListener
}
return v;
}

public void onClick(View v) {
Log.w(TAG, "something got clicked: ");
}

那么如何将数据库记录 ID 传递给处理程序,以便我可以使用数据库适配器将其删除?

最佳答案

您应该将记录的ID存储在Tag中,在您的 View 上调用setTag(),并在onclick调用getTag()

中读取

关于android - 将参数传递给 ListView 行中按钮上的 onclick 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3471613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com