0

I have ImageView with listItems from db, and every list items have two Buttons (add friend/rejectFriend). I am also using CursorAdapter, and inside of my adapter I have getView() method to set listeners and catch clicks on current item Button.

and here the question: I need to get some data from db for current item, when i catch click on item Button (add friend for example). so with help of getView() parameters I can get the position of ListView item, but how from getView() correct call cursor and getItemAtPosition ?

here is my getView() method from adapter class:

@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View view =  super.getView(position, convertView, parent);
        Button mAcceptNewFriend = (Button) view.findViewById(R.id.btn_itemFriendsAddNew_accept);
            Button mRejectNewFriend = (Button)view.findViewById(R.id.btn_itemFriendsAddNew_reject);

            mRejectNewFriend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mActivity, "user #" + position + " was removed", Toast.LENGTH_SHORT).show();

                }
            });
            mAcceptNewFriend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int friendId = mCursor.getInt(FriendsFragment.F_FRIEND_ID);
                }
            });
        return view;
    }
Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53

2 Answers2

1

Second possible solution:

Тry with the following solution: adding a cursor with the constructor of your CoursorAdapter and then use cursor.moveToPosition(position) in the getView() method: Get correct cursor in CustomCursor Adapater getView()

Community
  • 1
  • 1
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
0

When your parent view is a listView, you could try to add in the class where is your ListView filled in a method for the listItemClick and to get in it the cursor for example like this:

@Override
protected void onListItemClick(ListView l, View v, int position, long ida) {
   super.onListItemClick(l, v, position, ida);

   Cursor mycursor = (Cursor) getListView().getItemAtPosition(position); //I'm not sure for the getListView() part and I think that it could be (Cursor)l.getItemAtPosition(position); Please try with both suggestions if the first doesn't work for you
   Toast.makeText(mActivity, "mycursor.getString(1) " + mycursor.getString(1) +"   ", Toast.LENGTH_SHORT).show();
}
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • hm, cannot resolve onListItemClick in my fragment... can you please give more explicit answer how to do this stuff? – Stan Malcolm Jul 06 '15 at 09:20
  • try to add in your class definition the following implementation: `implements OnItemClickListener` – Gabriella Angelova Jul 06 '15 at 09:30
  • i have already implemented OnItemClickListener, but it provides not onListItemClick, but onItemClick... and when I am pressing button on listItem, onItemClick listener doesnot work.... – Stan Malcolm Jul 06 '15 at 09:49
  • Try to set the OnListItemClickListener like this for example: `listview.setOnItemClickListener(new onItemClickListener(){ @Override protected void onListItemClick(){ //here comes the code for the cursor from above } });` and in the Cursor line change getListView() with listView (the variable with your listView) – Gabriella Angelova Jul 06 '15 at 10:01
  • when i am calling new AdapterView.OnItemClickListener() it calls onItemClick, but not onListItemClick, and if i am trying to overwrite it - cannot resolve method – Stan Malcolm Jul 06 '15 at 10:13
  • hmmm then try with the onItemClick method like this for example: `@Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { Cursor tmpCursor = arg0.getItemAtPosition(arg2); }` – Gabriella Angelova Jul 06 '15 at 10:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82481/discussion-between-andriy-antonov-and-gabriella-angelova). – Stan Malcolm Jul 06 '15 at 10:59