listview项和数据库的rowid在删除listview项时不匹配

时间:2014-06-18 05:04:03

标签: android listview android-listview sqlite

这是MainActivity

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

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);
        }

    }

}

}

1 个答案:

答案 0 :(得分:2)

rowid of listview item and database doesn't match对于知道关系数据库如何工作的人来说,这是非常明显的。

数据库ID don't change(并且不应该),而ListView位置总是从0重新创建,而不会留下空白

由于您所谓的“ListView行ID”不存在:它是不是ID 。这是一个位置 它始终从0开始 在您的数据库中,行ID是真实的,不可变的,id。

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

db ID和ListView位置未按1:1 配对。


通过在自定义行中使用隐藏字段,您可以将db行ID存储在特定位置 因此,当您要删除该行时,可以检索该ID。

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

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

相关问题