【问题标题】:rowid of listview item and database doesn't match while deleting item of listview删除listview项目时listview项目和数据库的rowid不匹配
【发布时间】:2014-06-18 05:04:03
【问题描述】:

这里是 MainActivity

我使用导航抽屉从数据库中填充列表视图。当我单击列表视图中的项目时,它会启动一个新片段。但是当我删除检索笔记时,它与数据库和列表视图中的 rowid 不匹配。

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    list = (ListView) findViewById(R.id.left_drawer);
    mDbHelper = new NotesDbAdapter(this);

    mDbHelper.open();
    Log.d(tag, "connection succesfull");

    List<String> arraylist = mDbHelper.getData();

    arrayadapter = new ArrayAdapter<String>(this,

    R.layout.drawer_list_item, arraylist);

    list.setAdapter(arrayadapter);


    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    list.setOnItemClickListener(new DrawerItemClickListener());

NotesdbAdapter

 public List<String> getData() {
    String[] columns = new String[] { KEY_TITLE};
    Cursor c = mDb.query(DATABASE_TABLE, columns, null, null, null, null,
        null);
    String results = "";
    List<String> results1 = new ArrayList<String>();
    int iCM = c.getColumnIndex(KEY_TITLE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results1.add(c.getString(iCM));

    }
    return results1;

}

片段

 mDbHelper = new NotesDbAdapter(getActivity());
    mDbHelper.open();

    Log.i(tag, "connection sucessfull in planetfragment");

    Long i = getArguments().getLong(ARG_PLANET_NUMBER);

    TextView title = (TextView) rootView.findViewById(R.id.title);

    TextView body = (TextView) rootView.findViewById(R.id.body);

    Log.i(tag, "click item rowid is :[" + i + "]");

    if (i == 0||i!=0);


    {

        Log.i(tag,"row id after increment:["+i+"]");
        note = mDbHelper.fetchNote(i);
        startManagingCursor(note);
        {

            title.setText(note.getString(note
                    .getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));

            body.setText(note.getString(note
                    .getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));

        }
    }
    id=i;
    Log.i(tag," id value is :["+id+"]");
    Button deletebutton = (Button) rootView.findViewById(R.id.delete_note);
    deletebutton.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View arg0) {
            //Toast.makeText(getActivity(), R.string.item_click, Toast.LENGTH_LONG).show();
            Log.i(tag,"delete id value is :["+id+"]");
            if (note!=null) {
                note.close();
                note = null;
            }
            if (id!=null) {
                mDbHelper.deleteNote(id);
            }
            getActivity().finish();

        }
    });  

    return rootView;

}

private void startManagingCursor(Cursor note2) {
    // TODO Auto-generated method stub

}

public static class LineEditText extends TextView {
    // we need this constructor for LayoutInflater
    public LineEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.BLUE);
    }

    private Rect mRect;
    private Paint mPaint;

    @Override
    protected void onDraw(Canvas canvas) {

        int height = getHeight();
        int line_height = getLineHeight();

        int count = height / line_height;

        if (getLineCount() > count)
            count = getLineCount();

        Rect r = mRect;
        Paint paint = mPaint;
        int baseline = getLineBounds(0, r);

        for (int i = 0; i < count; i++) {

            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1,
                    paint);
            baseline += getLineHeight();

            super.onDraw(canvas);
        }

    }

}

}

【问题讨论】:

  • rowid of listview item and database doesn't match 这很显而易见。创建一个自定义行,其中有一个隐藏字段,用于存储 db rowId。并使用 that rowId 执行删除/更新
  • 我可以从 listView 中检索点击项目,但我无法删除它,因为当我看到日志时,另一个项目被删除而不是没有被点击。
  • 当然。重新阅读我的评论。
  • 如何自定义rowid有教程吗
  • 看看这个答案:stackoverflow.com/a/15832564/2649012。只需设置“标题”单元格visibility = "GONE" 并使用它来存储您的 rowID。

标签: android listview android-listview sqlite


【解决方案1】:

rowid of listview item and database doesn't match 这对于了解关系数据库如何工作的人来说是显而易见的。

数据库 ids don't change(不应该),而 ListView 位置总是从 0 重新创建,不留空隙

因为您所说的“ListView 行 id”不存在:它不是 id。这是一个职位。
它总是从 0 开始。
在您的数据库中,行 id 是一个真实的、不可变的 id。

想象它就像一个名字。
即:名为“45”的行将被删除。
但是名为“46”及更高的行不受影响(它们不会更改名称)。

db id 和 ListView 位置不是 1:1 配对。


通过在自定义行中使用隐藏字段,您可以将数据库行 ID 存储在特定位置。
因此,当您想删除那行时,您可以检索 THAT id。

因此,创建一个自定义行,其中有一个隐藏字段用于存储数据库 rowId。
并使用该 rowId 执行删除/更新

如果您需要了解如何为 ListView 创建自定义行项目,请查看此答案:stackoverflow.com/a/15832564/2649012:
只需设置“标题”单元格可见性 =“GONE”并使用它来存储您的 rowID。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多