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