在我使用listview id删除SQLite数据库中的一行后,listview会在同一位置显示错误的新项目内容

时间:2016-09-17 14:14:56

标签: android sqlite listview

这很难解释,但我会尝试。

我有一个Listview,显示当前保存在SQLite数据库中的所有项目。但Listview仅显示数据集行的部分内容。 单击列表视图项时,将启动一个新的活动(ResultEntryActivity),其中显示所有行内容。在该活动中,我希望用户能够在他/她点击按钮时删除该条目。然后,intent会将用户带回listview所在的SearchEntryActivity。 然后删除列表视图项,但是当我点击现在位于刚刚删除的项目的位置的项目时,ResultEntryActivity中不会显示任何内容。但不知何故,listview项目本身(显示部分内容)显示了正确的内容...... 有谁知道如何解决这一问题?我真的需要它来工作。

这些是SearchEntryActivity的重要方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    initDb();
    initListView();
    initAdapter();
    setItemListener();
}

@Override
protected void onDestroy() {
    myDb.close();
    super.onDestroy();
}

private void initDb() {
    myDb = HomeActivity.myDb;
    myDb.open();
}
@Override
protected void onResume(){
    super.onResume();
    getAdapter.swapItems(myDb.getAllEntryItems(0, "list"));
}

private void initListView() {
    listView = (ListView) findViewById(R.id.list);
}

private void initAdapter() {
    myList = myDb.getAllEntryItems(0, "list");
    Collections.sort(myList);
    getAdapter = new ListViewAdapter(this, myList);
    listView.setAdapter(getAdapter);
}

private void setItemListener() {

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(SearchActivity.this, ResultEntryActivity.class);
            id += 1;
            i.putExtra("entryId", id);
            i.putExtra("listItemId", position);
            startActivity(i);
        }
    });
}

这些是ResultEntryActivity的重要方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result_entry);

    initComponents();
    initDb();
    viewAll();
    deleteEntry();
}
private void deleteEntry() {
    deleteEntry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.startAnimation(buttonAnimation);
            showAlert();

        }
    });
}

private void showAlert() {
    AlertDialog.Builder passwordDialog = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater)ResultEntryActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.delete_alert_layout, null);

    Button yes = (Button)layout.findViewById(R.id.button_yes);
    Button no = (Button)layout.findViewById(R.id.button_no);

    dialog = passwordDialog.create();
    dialog.setView(layout);
    dialog.setCancelable(false);
        yes.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View view) {
                view.startAnimation(buttonAnimation);
                dialog.dismiss();
                myDb.deleteEntry(""+entryId+"");
                Toast.makeText(ResultEntryActivity.this, entryDeleted, Toast.LENGTH_LONG).show();
                Intent i = new Intent(ResultEntryActivity.this, SearchActivity.class);
                i.putExtra("position", entryId);
                startActivity(i);

            }
        });
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.startAnimation(buttonAnimation);
                //Eintrag soll nicht gelöscht werden -> Alert Dialog wird einfach geschlossen und nichts passiert.
                dialog.dismiss();
            }
        });
    dialog.show();
}

private void initDb() {
    myDb = HomeActivity.myDb;
    myDb.open();
}

private void initComponents() {
    imageView= (ImageView) findViewById(R.id.image_view);
    mapImageView = (ImageView) findViewById(R.id.map_image_view);
    dateView = (TextView) findViewById(R.id.result_date);
    dateView.setPaintFlags(dateView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    headlineView = (TextView) findViewById(R.id.result_headline);
    textView = (TextView) findViewById(R.id.result_text);
    editEntry = (ImageButton)findViewById(R.id.edit_this_entry);
    deleteEntry = (ImageButton)findViewById(R.id.delete_this_entry);
}

public void viewAll(){
    Bundle idBundle = getIntent().getExtras();
    entryId = (long) idBundle.get("entryId");
    System.out.println(entryId);
    myDb.getAllEntryItems(entryId, "entry");
}

这是来自我的数据库:

public ArrayList<EntryItem> getAllEntryItems(long id, String entryOrList) {
    if(entryOrList.equals("entry")){
        Cursor cursor = database.query(TABLE_NAME, new String[] {COL_ID, COL_DATE, COL_HEAD, COL_IMAGE, COL_MAP,COL_TEXT }, COL_ID + "=" + id +"", null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                String date = cursor.getString(INDEX_DATE);
                ResultEntryActivity.dateView.setText(date);
                String headline = cursor.getString(INDEX_HEAD);
                ResultEntryActivity.headlineView.setText(headline);
                String text = cursor.getString(INDEX_TEXT);
                ResultEntryActivity.textView.setText(text);
                byte[] image = cursor.getBlob(INDEX_IMAGE);
                if(image !=null){
                    ResultEntryActivity.imageView.setVisibility(View.VISIBLE);
                    ResultEntryActivity.imageView.setImageBitmap(DbBitmapUtility.getImage(image));
                }
                byte[] map = cursor.getBlob(INDEX_MAP);
                if(map != null){
                    ResultEntryActivity.mapImageView.setVisibility(View.VISIBLE);
                    ResultEntryActivity.mapImageView.setImageBitmap(DbBitmapUtility.getImage(map));
                }

            } while (cursor.moveToNext());
        }
        cursor.close();
    }else if(entryOrList.equals("list")){
        ArrayList<EntryItem> listItems = new ArrayList<EntryItem>();
        Cursor cursor = database.query(TABLE_NAME, new String[] { COL_ID, COL_DATE,
                COL_HEAD, COL_IMAGE }, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {

                String date = cursor.getString(INDEX_DATE);
                String headline = cursor.getString(INDEX_HEAD);
                if(cursor.getBlob(INDEX_IMAGE) != null){
                    byte[] image = cursor.getBlob(INDEX_IMAGE);
                    listItems.add(new EntryItem(date, headline, image, null, null));
                }else {
                    listItems.add(new EntryItem(date, headline, null, null, null));
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
        return listItems;
    }
    return null;
}

public void deleteEntry(String id){
    database.delete(TABLE_NAME, "ID = ?", new String[] {id});
}

我希望我添加了相关的所有内容。对不起,这太多了,但我真的需要帮助。 感谢。

0 个答案:

没有答案