我有如下列表视图。 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-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" />
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);
}
感谢。
菲利克斯
答案 0 :(得分:5)
只需在你的适配器中创建一个私有类,并在bindView方法中设置监听器就像
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.setOnClickListener(new OnItemClickListener(cur.getInt(idRow)));
}
private class OnItemClickListener implements OnClickListener {
private int position;
public OnItemClickListener(int position) {
super();
this.position = position;
}
@Override
public void onClick(View v) {
// position is an id.
}
}
答案 1 :(得分:1)
这里是一个代码,您可以通过该代码找到按钮的行位置
覆盖ModuleCursorAdapter中的方法
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView= super.getView(position,convertView,parent );
Button btnButtonOFF = (Button)convertView.findViewById(R.id.buttonOFF);
String tag = btnButtonOFF.getTag().toString()+","+position;
btnButtonOFF.setTag(tag);
}
现在在btnButtonOFFclicked监听器的onclick方法获取View&amp;的标签提取附加标签的位置 如果有用,请投票