问题
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;
}
回答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()
回答2:
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();
}
来源:https://stackoverflow.com/questions/31160352/how-to-get-getitematposition-in-getview