本文介绍了如何获得getItemAtPosition在getView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的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(位置) 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:18