更改数据库后ListView未更新

时间:2012-02-19 02:53:07

标签: android sqlite corresponding-records row-removal

由于糟糕的编程风格和Android经验不足,我真的很沮丧。很抱歉告诉你们。

该应用如何运作:

这是我的职业培训的todo应用程序。共有6列。 其中3个包含有关待办事项的信息,另外3个包含视图详细信息,编辑和删除屏幕。

当用户因设置notifyDataChange后出于某种原因点击删除时,我的屏幕未更新,删除的行仍显示。

关于这里发生了什么的任何想法?我已经尝试了大约3个小时的许多解决方案

如果有点繁琐的话,代码会在这里发布。

全班ListView s:

package com.DCWebMakers.Vairon;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ManageAppointment extends Activity {
    ListView rowLi, whenLi, postedLi, detailsLi, editLi, removeLi;
    ArrayAdapter<String> whenAdapter, postedAdapter, detailsAdapter,
            editAdapter, removeAdapter;
    ArrayAdapter<Integer> rowAdapter;
    final AppointmentInfo information = new AppointmentInfo(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        /*
         * The ListViews created here are not the proper way to make ListViews.
         * This is for testing purposes and will be updated for efficiency. The
         * remove also doesn't work properly
         */

        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage_appointment);
        initVariables();
        try {

            databaseManagement();

            detailsLi.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> aV, View v, int pos,
                        long arg3) {
                    // TODO Auto-generated method stub

                    Intent openDetails = new Intent(
                            "com.DCWebMakers.Vairon.APPOINTMENTDETAILS");
                    openDetails.putExtra("position", pos);
                    startActivity(openDetails);

                }
            });
            editLi.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> aV, View v, int pos,
                        long arg3) {
                    // TODO Auto-generated method stub

                    Intent openEdit = new Intent(
                            "com.DCWebMakers.Vairon.EDITAPPOINTMENT");
                    openEdit.putExtra("position", pos);
                    startActivity(openEdit);
                    notifyChangesToAdapters();

                }
            });

            removeLi.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> aV, View v, int pos,
                        long arg3) {
                    // TODO Auto-generated method stub
                    databaseManagement();

                    information.open();
                    information.delete(pos);
                    information.close();


                    Dialog sucDeleted = new Dialog(ManageAppointment.this);
                    sucDeleted.setTitle("Sucesfully deleted");
                    TextView tvDintWorked = new TextView(ManageAppointment.this);
                    tvDintWorked.setText("The appointment at position:" + pos
                            + " was sucesfully deleted");
                    sucDeleted.setContentView(tvDintWorked);
                    sucDeleted.show();

                    notifyChangesToAdapters();
                }
            });

        } catch (Exception e) {

            Dialog showError = new Dialog(this);
            showError.setTitle("Error");
            TextView tvDintWorked = new TextView(this);
            String error = e.toString();
            tvDintWorked.setText(error);
            showError.setContentView(tvDintWorked);
            showError.show();
        }

    }

    public void initVariables() {
        rowLi = (ListView) findViewById(R.id.rowList);
        whenLi = (ListView) findViewById(R.id.whenList);
        postedLi = (ListView) findViewById(R.id.postedList);
        detailsLi = (ListView) findViewById(R.id.detailsList);
        editLi = (ListView) findViewById(R.id.editList);
        removeLi = (ListView) findViewById(R.id.removeList);
    }

    @Override
    protected void onPause() { // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        notifyChangesToAdapters();
    }

    private void notifyChangesToAdapters() {
        // TODO Auto-generated method stub

        rowAdapter.notifyDataSetChanged();
        whenAdapter.notifyDataSetChanged();
        postedAdapter.notifyDataSetChanged();
        detailsAdapter.notifyDataSetChanged();
        editAdapter.notifyDataSetChanged();
        removeAdapter.notifyDataSetChanged();

    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent mainIntent = new Intent("com.DCWebMakers.Vairon.MAINMENU");
        startActivity(mainIntent);
    }

    public void databaseManagement() {
        information.open();
        int primaryKey = information.getKeys();
        String when[] = information.getWhen();
        String posted[] = information.getPosted();
        String details[] = new String[primaryKey];
        Integer rowNumber[] = new Integer[primaryKey];
        String edit[] = new String[primaryKey];
        String delete[] = new String[primaryKey];

        for (int set = 0; set < rowNumber.length; set++) {
            rowNumber[set] = (set);
        }
        for (int set = 0; set < details.length; set++) {
            details[set] = ("Details");
        }
        for (int set = 0; set < edit.length; set++) {
            edit[set] = ("Edit");
        }

        for (int set = 0; set < delete.length; set++) {
            delete[set] = ("Delete");
        }

        information.close();

        rowAdapter = new ArrayAdapter<Integer>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, rowNumber);
        whenAdapter = new ArrayAdapter<String>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, when);
        postedAdapter = new ArrayAdapter<String>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, posted);
        detailsAdapter = new ArrayAdapter<String>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, details);
        editAdapter = new ArrayAdapter<String>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, edit);
        removeAdapter = new ArrayAdapter<String>(ManageAppointment.this,
                android.R.layout.simple_list_item_1, delete);

        rowLi.setAdapter(rowAdapter);
        whenLi.setAdapter(whenAdapter);
        postedLi.setAdapter(postedAdapter);
        detailsLi.setAdapter(detailsAdapter);
        editLi.setAdapter(editAdapter);
        removeLi.setAdapter(removeAdapter);
    }

}

删除方法:

public void delete(int position) {
    theDatabase.beginTransaction();
    try {
        theDatabase
                .delete(DATABASE_TABLE, KEY_ROWID + "=" + position, null);
        theDatabase.setTransactionSuccessful();
    } catch (SQLiteException e) {
        // TODO: handle exception
        e.printStackTrace();
    } finally {
        theDatabase.endTransaction();
        theDatabase.close();
    }
}

2 个答案:

答案 0 :(得分:0)

notifyDataSetChanged之前

您需要从List

中删除该条目

答案 1 :(得分:0)

从数据库中删除时是否看到任何异常?并且您还需要在通知之前从全局列表中删除pos条目。