【发布时间】:2014-09-05 23:03:01
【问题描述】:
我有一个列表视图,其中包含由数据库填充的每个列表项的相对视图,并包含两个按钮。 当按下列表视图项中的一个按钮时,我需要在列表视图中获取该行的 id。换句话说,我试图找到与 onItemClickListener() 的 onItemClick 方法中的最后一个参数相同的东西,这将返回整个项目的点击。但是我只是在监听 listview 项目中的按钮,所以我无法使用 onItemClickListener()。
单击按钮时会调用此方法
public void plusClick(View v)
{
//get the row the clicked button is in
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
//i need to get the id of this RelativeLayout item in the list view
TextView child = (TextView)vwParentRow.getChildAt(2);
Button btnChild = (Button)vwParentRow.getChildAt(1);
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
它需要找到我可以传递 ass arg 的相对布局的 id
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
创建一个包含此列表视图项中数据的新光标
【问题讨论】:
-
如何为每个按钮设置
OnClickListener? -
"创建一个包含此列表视图项中数据的新游标"...嗯...为什么需要一个新游标?你不使用
CursorAdapter吗?如果没有,我强烈建议你这样做。听起来您正在尝试在这里重新发明轮子。 -
按钮当前设置为xml android:onClick="plusClick"
-
@sam:最简单的解决方案是使用适当的数据(即 id,如果您只对这些数据感兴趣)标记按钮。或者,自己设置
OnClickListener。然后,您可以在即构造函数中将数据作为额外参数提供,或者将侦听器实现为匿名类,直接引用相关数据。
标签: java android listview android-listview