3

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

FelixA
  • 127
  • 5
  • 14
  • You cannot specify onClickListeners for Button or ImageButton in ListView. Set onClickListener to ListView and get the button or layout using the row number. – KK_07k11A0585 Dec 05 '11 at 12:54
  • @KK_07k11A0585 why can't we set onClickListener for Button on ListView? – Lalit Poptani Dec 05 '11 at 12:58
  • Becoz it will override the OnClickistener() the list view becomes cannot be clickable. It looses it onclick property. You cannot set OncLick Listener to any child inside list view – KK_07k11A0585 Dec 05 '11 at 13:01
  • To handle this I set the Button in the .xml as: android:focusable="false". See my comment above. – FelixA Dec 05 '11 at 13:01
  • @KK_07k11A0585 When I put the onClickListener to the ListViewActivity. How can I read there the entry from the database without the cursor? – FelixA Dec 05 '11 at 13:06
  • What you are looking to implement tell me clearly i will provide you the solution if i can – KK_07k11A0585 Dec 05 '11 at 13:10
  • @KK_07k11A0585. I edit my post. Now I put the btnButtonOFFclicked-Method in the main activity. I want to implement there the toast which I have put after "//". – FelixA Dec 05 '11 at 13:18
  • @KK_07k11A0585 we can set OnClickListener as FelixA said. – Lalit Poptani Dec 06 '11 at 02:33

2 Answers2

5

Just create a private class inside you adapter like this and set listener on button in bindView method its will be like

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.
    }
}
s_id
  • 446
  • 3
  • 10
  • I tried the following. `code@Override public void onClick(View v) { mCursor.moveToPosition(position); Toast.makeText(mContext,mCursor.getString(1), Toast.LENGTH_LONG).show(); }` But that are not the correct values, because when a database entry was delited, the first shown row must not hava the position id one. – FelixA Dec 06 '11 at 18:37
  • @FelixA can you post a code where you create a cursor for your adapter ModuleCursorAdapter? – s_id Dec 06 '11 at 18:47
  • I added it to my post. If you whish, I can also post the whole MainActivity. It comes directly from the google notepad tutorial. – FelixA Dec 06 '11 at 19:00
  • i'm sorry but i'm interested about what is inside this method mDbHelper.fetchAllNotes(); – s_id Dec 06 '11 at 19:03
  • No problem. But I have just found an answer. 1. change to `btnButtonOFF.setOnClickListener(new OnItemClickListener(cur.getPosition())); ` 2 `mCursor.moveToPosition(position); Toast.makeText(mContext,mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE)), Toast.LENGTH_LONG).show(); ` That's it. But thank you so much!!! But I will post fetchAllNotes() above. – FelixA Dec 06 '11 at 19:09
  • Now i copied fetchAllNotes() to my post. Thank you for your help. It was so useful! No I understand this stuff. – FelixA Dec 06 '11 at 19:21
1

here is code by which you can able to find out row position of button

override the method in 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);

}

now in onclick method of btnButtonOFFclicked listener get tag of View & extract the position attached with tag if helpful then please vote

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54
  • Thank you. 3 Question. 1. Is it correct, that the return value for the getView method is "return convertView"? 2. I set the tag like this: "btnButtonOFF.setTag(view.getTag());". Is it correct?. 3. How can I access to the right database entry "NotesDbAdapter.KEY_TITLE" in the OnClickListener without the cursor. Can you help me with a code for the OnClickListener? – FelixA Dec 06 '11 at 11:56
  • @ Satya. "btnButtonOFF.setTag(view.getTag());" can not be correct, because the programm stopps with a null pointer exeption und the debugger jumps to "String tag = btnButtonOFF...". I hope you can help me. – FelixA Dec 06 '11 at 12:17
  • @FelixA actually I forgot to add `btnButtonOFF.setTag(tag);` this statement check ans now – Vishal Pawar Dec 06 '11 at 12:39
  • regarding your comments 1. yes you can use convertView but if you modify this then same object of convertView will be reused after few elements of adapter (means if you are able to see 10 items of adapter in screen then if you scroll your list your 1st item of adapter returned as ConvertView ) 2. is correct 3. you can do `listview.getAdapter().getItem(position)` where position you can extract from the tag that you set for `btnButtonOFF` – Vishal Pawar Dec 06 '11 at 12:45