如何在getView中获取getItemAtPosition?
How to get getItemAtPosition in getView?
我有 ImageView
和来自数据库的 listItems,每个列表项都有两个 Buttons
(添加 friend/rejectFriend)。
我也在使用 CursorAdapter
,在我的适配器内部,我有 getView()
方法来设置侦听器并捕获对当前项目 Button
.
的点击
这里的问题是:
当我点击项目 Button
(例如添加朋友)时,我需要从数据库中获取当前项目的一些数据。所以在 getView()
参数的帮助下我可以获得 ListView
项目的位置,但是如何从 getView()
正确调用光标和 getItemAtPosition
?
这是我的 getView()
来自适配器 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;
}
当您的父视图是 listView 时,您可以尝试在 class 中添加您的 ListView 填充 listItemClick 的方法并在其中获取光标,例如:
@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();
}
第二种可能的解决方案:
尝试以下解决方案:使用 CoursorAdapter
的构造函数添加游标,然后在 getView()
方法中使用 cursor.moveToPosition(position)
:Get correct cursor in CustomCursor Adapater getView()
我有 ImageView
和来自数据库的 listItems,每个列表项都有两个 Buttons
(添加 friend/rejectFriend)。
我也在使用 CursorAdapter
,在我的适配器内部,我有 getView()
方法来设置侦听器并捕获对当前项目 Button
.
这里的问题是:
当我点击项目 Button
(例如添加朋友)时,我需要从数据库中获取当前项目的一些数据。所以在 getView()
参数的帮助下我可以获得 ListView
项目的位置,但是如何从 getView()
正确调用光标和 getItemAtPosition
?
这是我的 getView()
来自适配器 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;
}
当您的父视图是 listView 时,您可以尝试在 class 中添加您的 ListView 填充 listItemClick 的方法并在其中获取光标,例如:
@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();
}
第二种可能的解决方案:
尝试以下解决方案:使用 CoursorAdapter
的构造函数添加游标,然后在 getView()
方法中使用 cursor.moveToPosition(position)
:Get correct cursor in CustomCursor Adapater getView()