2014-02-25 35 views
4

我創建了一個打開對話框的佈局,進入對話框後,用戶選擇確定/取消。我想刷新列表視圖再次查詢數據如何在關閉對話框後刷新listView到?

這裏是我的佈局,將打開對話框:

button.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       Intent myIntent = new Intent(SchoolActivity.this, InsertSchool.class); 
       update(); 
       startActivity(myIntent); 
       updateList(); 
      } 
     }); 
     update(); 
     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); 

點擊該按鈕後,我想刷新列表視圖。這裏是對話框(我認爲刷新將在ok &取消按鈕上完成)。

  ib.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent=new Intent(InsertSchool.this, SchoolActivity.class); 
       startActivity(intent); 
      } 
     }); 
     update(); 
     mySQLiteAdapter = new Database(this); 
     mySQLiteAdapter.openToWrite(); 
     cursor = mySQLiteAdapter.queueSchoolAll(); 

     ib1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       String data1 = ie1.getText().toString(); 
       String data2 = ie2.getText().toString(); 
       mySQLiteAdapter.insertSchool(data1, data2); 
       updateList(); 

       Toast.makeText(getApplicationContext(), "Saved", 
          Toast.LENGTH_SHORT).show(); 

       Intent myIntent = new Intent(InsertSchool.this, SchoolActivity.class); 
       update(); 
       startActivity(myIntent); 

       mySQLiteAdapter.close(); 
       } 
      } 
     }); 
+0

使用拉刷新庫therefreshData()方法.. https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&sqi=2&ved=0CCUQFjAA&url=https%3A%2F%2Fgithub.com%2Fchrisbanes%2FAndroid-PullToRefresh&ei=jk0MU7ryE4mRrAelyoCIDA&usg= AFQjCNFfDp36h1omRJIIVOtNBUQaFjigYw&bvm = bv.61725948,d.bmk&cad = rja – rajshree

+0

我會試試看...感謝您的信息 – CJanon

+0

請提供'update()'和'updateList()' –

回答

2

要刷新listView你應該打電話給你的列表的適配器上的方法notifyDataSetChanged()。當調用該方法取決於您...也許在您的onClickListener

+0

我試過了,但沒有任何幫助。 – CJanon

+0

它應該。也許你必須把它放在你的'updateList()'方法的末尾。雖然我現在不知道它是什麼樣子 –

+0

只是編寫一個內容提供程序,一切都會自動更新(follow notepad tutorial) – pskink

1

我簡單地使用這種技術:在主要活動把這個

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    refreshData(); 
} 

儘快的AlertDialog被駁回,它調用refreshs ListView控件

+0

你給出了正確的答案謝謝你工作很不錯 – Sam

相關問題