Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 错误的光标位置_Android_Sqlite - Fatal编程技术网

Android 错误的光标位置

Android 错误的光标位置,android,sqlite,Android,Sqlite,我正在使用ContentProvider和SQlite数据库和 我想在列表视图中显示我的数据库内容 但是当我想选择已单击的相应视图时,cursor.getPosition()返回的位置是错误的 这是我的代码: public class ClipsAdapter extends CursorAdapter implements View.OnClickListener { private static final String TAG = ClipsAdapter.class.getCa

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

我想在列表视图中显示我的数据库内容

但是当我想选择已单击的相应视图时,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;

    }

}

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

谢谢。尽管如此,这并不是解决方案。我解决了实现AdapterView.OnItemClickListener的问题,并使用了这篇文章中的“为什么我的项目视图不再可点击?”一节->