错误的光标 getPosition [英] Wrong cursor getPosition

查看:22
本文介绍了错误的光标 getPosition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ContentProvider 和 SQlite 数据库做一个 Android 应用程序

I'm doing an Android application using ContentProvider and SQlite Database and

我想在 ListView 中显示我的数据库内容.

I want to show my database content in a ListView.

但是当我想选择已经点击的对应视图时,cursor.getPosition()返回的位置是错误的.

But when I want to select the corresponding view that has been clicked, the position return by cursor.getPosition() is wrong.

这是我的代码:

public class ClipsAdapter extends CursorAdapter implements View.OnClickListener {

    private static final String TAG = ClipsAdapter.class.getCanonicalName();

    public interface SelectedFragmentListener{
        void onSelectedFragment(int fragment_index,int id);
        void onMenuOverflow(View v);
    }

    private SelectedFragmentListener listener;
    private LayoutInflater mInflater;
    private final int mTitleIndex;
    private final int mIdIndex;
    private final int mInternalIdIndex;
    private final int mFaviconUrlIndex;
    private final int mIdCreatorIndex;
    private final int mInternalStatusIndex;
    private Activity mActivity;

    private int userId;
    private int position;
    private int identifier;

    private KipptDAO mKipptDAO = KipptDAO.getInstance();


    public static final String[] PROJECTION_CLIPS = new String[] {
            DatabaseContract.ClipEntry._ID,
            DatabaseContract.ClipEntry.ID_CLIP_SERVER, DatabaseContract.ClipEntry.ID_SERVER_CREATOR,
            DatabaseContract.ClipEntry.TITLE, DatabaseContract.ClipEntry.FAVICON_URL,
            DatabaseContract.ClipEntry.STATUS_SYNC};


    public ClipsAdapter(Activity activity, SelectedFragmentListener listener) {
        super(activity, getManagedCursor(activity), true);

        mActivity = activity;
        mInflater = LayoutInflater.from(activity);
        final Cursor c = getCursor();
        this.listener = listener;
        this.position = -1;

        mIdIndex = c
                .getColumnIndexOrThrow(DatabaseContract.ClipEntry._ID);

        mInternalIdIndex = c
                .getColumnIndexOrThrow(DatabaseContract.ClipEntry.ID_CLIP_SERVER);
        mTitleIndex = c
                .getColumnIndexOrThrow(DatabaseContract.ClipEntry.TITLE);
        mInternalStatusIndex = c
                .getColumnIndexOrThrow(DatabaseContract.ClipEntry.STATUS_SYNC);

        mFaviconUrlIndex = c.getColumnIndexOrThrow(DatabaseContract.ClipEntry.FAVICON_URL);

        mIdCreatorIndex = c.getColumnIndexOrThrow(DatabaseContract.ClipEntry.ID_SERVER_CREATOR);


    }

    private static Cursor getManagedCursor(Activity activity) {
        return activity.getContentResolver().query(UserContentProvider.CLIP_TABLE_CONTENT_URI,
                PROJECTION_CLIPS,
                DatabaseContract.ClipEntry.STATUS_SYNC + " != "
                        + StatusFlags.DELETE,
                null,
                null
                );
    }

    @Override
    public void bindView(View view, Context context, Cursor c) {

        final ViewHolder holder = (ViewHolder) view.getTag();
        holder.title.setText(c.getString(mTitleIndex));

        userId = c.getInt(mIdCreatorIndex);


        holder.fullname.setText(mKipptDAO.isUserInDb(mActivity.getContentResolver(),userId).getFull_name());
        Utils.downloadResources(mKipptDAO.isUserInDb(mActivity.getContentResolver(),
                c.getInt(mIdCreatorIndex)).getAvatar_url(), holder.userAvatar, mActivity, TAG);
        Utils.downloadResources(mKipptDAO.isMediaInDb(mActivity.getContentResolver(),c.getInt(mInternalIdIndex)).getImages()
                .getTile().getUrl(),holder.favicon,mActivity,TAG);

        holder.clip.setOnClickListener(this);
        holder.userAvatar.setOnClickListener(this);
        holder.menuOverflow.setOnClickListener(this);


    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view;
        view = mInflater.inflate(R.layout.list_view_item_clips, parent,
                false);

        ViewHolder holder = new ViewHolder();
        holder.fullname = (TextView) view.findViewById(R.id.clip_user_creator);
        holder.title = (TextView) view.findViewById(R.id.clip_title);
        holder.favicon = (ImageView) view.findViewById(R.id.clip_favicon_image);
        holder.userAvatar = (RoundedImageView) view.findViewById(R.id.clip_creator_profile_image);

        holder.clip = (LinearLayout) view.findViewById(R.id.linear_view_clip);

        holder.menuOverflow = (ImageButton) view.findViewById(R.id.menu_overflow);

        view.setTag(holder);

     /*I was trying to use this.position = cursor.getInt(mIdIndex) but it's wrong too*/
        this.position = cursor.getPosition();


        return view;
    }

    @Override
    public void onClick(View v) {
        int idFragment=0,id=0;

        switch (v.getId()){
            case R.id.menu_overflow:
                /*TODO show popup menu*/
                listener.onMenuOverflow(v);
                break;

            case R.id.clip_creator_profile_image:
                idFragment = MainActivity.PROFILE_INDEX;
                id = this.userId;
                listener.onSelectedFragment(idFragment,id);
                break;

            case R.id.linear_view_clip:
                idFragment = MainActivity.CLIP_INDEX;
                id = position;
                listener.onSelectedFragment(idFragment,id);
                break;


        }
    }

    @Override
    public int getCount() {
        return getCursor().getCount();
    }

    private static class ViewHolder {
        TextView fullname;
        TextView title;
        RoundedImageView userAvatar;
        ImageView favicon;
        LinearLayout clip;
        ImageButton menuOverflow;

    }

}

推荐答案

您只有一个适配器实例,因此只有一个 position 成员变量.每次调用 newView() 都会重新分配变量.如果你想存储一个列表项的索引,把它放在视图持有者而不是适配器中.

You have only one adapter instance and hence one position member variable. The variable is reassigned for each call to newView(). If you want to store the index of a list item, put it in the view holder instead of adapter.

这篇关于错误的光标 getPosition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆