2014-02-22 70 views
0

在我的對話框中修改,只有刪除工作正常,刷新良好...我的添加對話框和編輯對話框工作正常,但不刷新list-view。我怎樣才能刷新這個?不能刷新listView,出了什麼問題?

下面是我的代碼片段。

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

}

任何幫助表示讚賞

+0

使用notifyDatasetChanged()刷新列表視圖。 – InnocentKiller

回答

2

要UDPATE新數據的列表視圖,使用

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

感謝您的回答。 ..我會在哪裏插入那行代碼? – CJanon

+0

我更新了我的答案。只要調用'updateList()'無論何時何地想用修改的數據刷新列表。 –

+0

由於您在StackOverflow中出現了新的內容,如果您發現答案有幫助,請點擊「^」投票答覆,並且不要忘記通過點擊答案右側的「綠色勾號」標記來接受答案。這將幫助其他人尋找相同的解決方案。 –

1

在updateList,使這一變化:

private void updateList(){ 

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

} 

For更多的,你可以看這個:

Google I/O Video