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;
}
Second possible solution:
Тry with the following solution: adding a cursor with the constructor of your
CoursorAdapter
and then usecursor.moveToPosition(position)
in thegetView()
method: Get correct cursor in CustomCursor Adapater getView()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: