带有 TextView 和 Button 的列表视图。单击按钮的 RowId
我有一个如下所示的列表视图。 TextView 中的文本来自数据库。
-------------------------
TextView Button
-------------------------
当我单击按钮时,我想在 Toast 中显示该行的 TextView 中的文本。
我的问题如下:当我单击按钮时,我将显示由光标选择的行文本。我没有显示按钮所在行的文本。我知道问题出在 mCursor 变量上。
我不知道如何解决它。有人有想法吗?
这是我的 ModulCursorAdapter:
public class ModuleCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
private Context mContext;
private Cursor mCursor;
public ModuleCursorAdapter(Context context, Cursor cur) {
super(context, R.layout.notes_row, cur);
this.mContext = context;
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = LayoutInflater.from(context);
return li.inflate(R.layout.notes_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
this.mCursor = cur;
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);
tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
int idRow = cur.getColumnIndex(NotesDbAdapter.KEY_ROWID);
btnButtonOFF.setTag(cur.getInt(idRow));
btnButtonOFF.setOnClickListener(btnButtonOFFclicked);
}
@Override
public void onClick(View view) {
}
private OnClickListener btnButtonOFFclicked = new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();
}
};
}
注释
xml 文件中的按钮:
<Button android:id="@+id/buttonOFF"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="OFF" />
<Button android:id="@+id/buttonOFF2"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:onClick="btnButtonOFFclicked"
android:text="ON" />
MainActivity 中的 OnClickMethod
public class MainActivity extends ListActivity {
.....
public void btnButtonOFFclicked(View view)
{
Toast.makeText(MainActivity.this,"Test", Toast.LENGTH_LONG).show();
//Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();
}
}
MainActivity
public class MainActivity extends ListActivity {
...
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
adapter = new ModuleCursorAdapter(this, notesCursor);
setListAdapter(adapter);
}
...
}
Notes.DbAdapter.fetchAllNotes() 方法
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_DEVICETYPE, KEY_HOMECODE, KEY_DEVICECODE}, null, null, null, null, null);
}
谢谢。
菲利克斯
I hava a listview like the following. The text in the TextView comes from a database.
-------------------------
TextView Button
-------------------------
When I click on the button, I want to show the text in the TextView of this row in a Toast.
My question is the following: When I click on the button, I am showing the text of the row, which is selected by the cursor. I am not showing the text of the row where the button is. I know the problem is the mCursor variable.
I don't know how to fix it. Has anybody an idea?
Here is my ModulCursorAdapter:
public class ModuleCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
private Context mContext;
private Cursor mCursor;
public ModuleCursorAdapter(Context context, Cursor cur) {
super(context, R.layout.notes_row, cur);
this.mContext = context;
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = LayoutInflater.from(context);
return li.inflate(R.layout.notes_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
this.mCursor = cur;
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);
tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
int idRow = cur.getColumnIndex(NotesDbAdapter.KEY_ROWID);
btnButtonOFF.setTag(cur.getInt(idRow));
btnButtonOFF.setOnClickListener(btnButtonOFFclicked);
}
@Override
public void onClick(View view) {
}
private OnClickListener btnButtonOFFclicked = new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();
}
};
}
Comment
The Button in the xml-File:
<Button android:id="@+id/buttonOFF"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:text="OFF" />
<Button android:id="@+id/buttonOFF2"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false"
android:onClick="btnButtonOFFclicked"
android:text="ON" />
The OnClickMethod in the MainActivity
public class MainActivity extends ListActivity {
.....
public void btnButtonOFFclicked(View view)
{
Toast.makeText(MainActivity.this,"Test", Toast.LENGTH_LONG).show();
//Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show();
}
}
MainActivity
public class MainActivity extends ListActivity {
...
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
adapter = new ModuleCursorAdapter(this, notesCursor);
setListAdapter(adapter);
}
...
}
The Notes.DbAdapter.fetchAllNotes() method
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_DEVICETYPE, KEY_HOMECODE, KEY_DEVICECODE}, null, null, null, null, null);
}
Thanks.
Felix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在适配器内创建一个私有类,如下所示,并在 bindView 方法中的按钮上设置侦听器,就像这样
Just create a private class inside you adapter like this and set listener on button in bindView method its will be like
这里是代码,您可以通过它找到按钮的行位置,
现在可以在 btnButtonOFFclicked 侦听器的 onclick 方法中覆盖 ModuleCursorAdapter 中的方法,
获取 View & 的标签。提取带有标签的位置
如果有帮助请投票
here is code by which you can able to find out row position of button
override the method in ModuleCursorAdapter
now in onclick method of btnButtonOFFclicked listener get tag of View & extract the position attached with tag
if helpful then please vote