无法刷新listView,有什么问题?

时间:2014-02-22 08:14:37

标签: android android-listview refresh

在我的对话框修改中,只有删除效果很好并且刷新良好...我的添加对话框和编辑对话框效果很好但不刷新list-view。我该怎么刷新呢?

以下是我的代码段。

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.school_screen);
        listContent = (ListView)findViewById(R.id.listView1);
        mySQLiteAdapter = new Database(this);
        mySQLiteAdapter.openToWrite();

        cursor = mySQLiteAdapter.queueSchoolAll();

        // Locate the button in activity_main.xml
        button = (Button) findViewById(R.id.add_school_button);

        // Capture button clicks
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) { 
                updateList();
                Intent myIntent = new Intent(SchoolActivity.this, InsertSchool.class);
                startActivity(myIntent);

            }
        });
        cursor.requery();
        String[] from = new String[]{Database.KEY_ID2, Database.KSCHOOL, Database.KSCHOOLCODE};
        int[] to = new int[]{R.id.rid, R.id.rt1, R.id.rt2};

        cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row_school, cursor, from, to);
        listContent.setAdapter(cursorAdapter);
        listContent.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
                final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KEY_ID2));
                sdid.sdid(item_id);
                Intent intent=new Intent(SchoolActivity.this,DetailsSchool.class);
                startActivity(intent);  
                }

            });
        cursor.requery();
            listContent.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

                // TODO Auto-generated method stub

                Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
                final int item_id = cursor.getInt(cursor.getColumnIndex(Database.KEY_ID2));
                  AlertDialog.Builder myDialog  = new AlertDialog.Builder(SchoolActivity.this);
                  myDialog.setTitle("Modify");
                  myDialog.setMessage("Do you want to edit or delete?");
                  myDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface arg0, int arg1) {
                            mySQLiteAdapter.delete_byID2(item_id);
                            updateList();
                            Intent intent=new Intent(SchoolActivity.this,SchoolActivity.class);
                            startActivity(intent);
                        }
                  });

                    myDialog.setNegativeButton("Edit", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface arg0, int arg1) {
                            updateSchool.id(item_id);
                            updateList();
                            Intent intent=new Intent(SchoolActivity.this, UpdateSchool.class);
                            startActivity(intent);
                         }
                    });

                    myDialog.show();
            }
        });
            cursor.requery();
 }
 private void updateList(){
        cursor.requery();
 }

}

感谢任何帮助

2 个答案:

答案 0 :(得分:2)

要使用新数据更新列表视图,请使用

private void updateList(){
    cursorAdapter.notifyDatasetChanged();
}

答案 1 :(得分:1)

在您的updateList中

进行此更改:

private void updateList(){

List<Data> newData = getYourNewData();
mAdapter.setList(yourNewList);
cursorAdapter.notifyDataSetChanged();   

}

更多信息,您可以观看:

Google I/O Video