问题描述
我的ImageView
与DB listItems中,每一个列表项有两个按钮
(添加好友/ rejectFriend) 。
我也使用的CursorAdapter
,和我的适配器里面我有 getView()
方法来设置监听器和捕捉点击当前项目按钮
。
和这里的问题是:
我需要从数据库中获取一些数据对当前选项,当我赶上点击项目按钮
(加为好友为例)。所以用 getView()
参数的帮助,我可以从<$ C $获得的ListView
项目,但如何的位置C> getView()正确的调用光标和 getItemAtPosition
?
下面是适配器类我的 getView()
方法:
@覆盖
公共查看getView(最终诠释的立场,观点convertView,最终的ViewGroup父){
查看查看= super.getView(位置,convertView,父母);
按钮mAcceptNewFriend =(按钮)view.findViewById(R.id.btn_itemFriendsAddNew_accept);
按钮mRejectNewFriend =(按钮)view.findViewById(R.id.btn_itemFriendsAddNew_reject); mRejectNewFriend.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
Toast.makeText(mActivity,用户#+位置+被删除,Toast.LENGTH_SHORT).show(); }
});
mAcceptNewFriend.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
INT friendId = mCursor.getInt(FriendsFragment.F_FRIEND_ID);
}
});
返回视图。
}
第二个可能的解决方案:
Тry用以下解决方案:将光标与 CoursorAdapter
,然后用 cursor.moveToPosition(位置)$ C的构造$ C>在
getView()
方法:Get正确的光标的CustomCursor Adapater getView()
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 use cursor.moveToPosition(position)
in the getView()
method: Get correct cursor in CustomCursor Adapater getView()
这篇关于如何获得getItemAtPosition在getView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!