错误的游标getPosition

时间:2013-11-18 14:23:03

标签: android sqlite

我正在使用ContentProvider和SQlite数据库和

进行Android应用程序

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

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

这是我的代码:

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;

    }

}

1 个答案:

答案 0 :(得分:0)

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