2011-05-23 100 views
0

在我的應用中,當用戶點擊添加菜單按鈕時,列表視圖顯示填充了從文本文件加載的項目。 所以現在用戶可以添加一個項目到列表視圖。將它添加到數組後,新項目被寫出到文本文件中,但沒有進入列表視圖,因爲我想通過將文件讀入數組,然後使用它填充ListView來完成此操作。問題是這不起作用。 arrayadapter填充位於onCreate(Bundle savedInstanceState){}方法中,但添加在其外部,因爲它是通過按菜單調用的。Android列表視圖刷新

public class Connection extends Activity { 
ListView lv1; 
ArrayList<String> assignArr0 = new ArrayList<String>(); 
ArrayList<String> assignArr00 = new ArrayList<String>(); 
ArrayAdapter<String> adapter1; 
ArrayAdapter<String> adapter2; 

public void onCreate(Bundle savedInstanceState) { 
... 
//filereading to assingArr0. Lines of the file are added to the listview. 
lv1 = (ListView) findViewById(R.id.ListView01); 
adapter1 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr0); 
lv1.setAdapter(adapter1); 
adapter1.notifyDataSetChanged(); 

//now, if there were any lines in the text file, the ListView is populated with them. 
} 

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.Menu1: 
//this is where user adds an item to an array and item gets written out to the text file 
//and this is also the part where the listview should be cleared, the lines of the text file should be read into an array, and the listview should be populated with the items of that array. 

//The filereading is ok. Lines are read into assignArr00. Now what? 

break; 
    } 
    return true; 
} 
} 

這是我試圖做到這一點線被讀入assignArr00後的方式:

if (fileisempty == false) //i set this boolean var at the beginning 
      { 
       adapter1.clear(); 
      // adapter1 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00); 
       //lv1.setAdapter(adapter1); 
       //adapter1.notifyDataSetChanged(); 
      } 
      else 
      { 
       adapter1 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00); 
       lv1.setAdapter(adapter1); 
       adapter1.notifyDataSetChanged(); 
       } 
      } 

在強制關閉這最後一個碼結果的其他部分(不要忘記,這是第一次運行,因此文本文件是空的,這就是爲什麼其他部分被激活。

我不是很肯定這件事。 也許聲明是在一個錯誤的地方。

任何幫助表示讚賞。

編輯:我設法解決它。解決方案可以在下面找到。

+0

你確定'assignArr00'正在填充嗎?你可以發佈該代碼嗎? – 2011-05-23 19:49:51

+0

請發佈logcat輸出 – 2011-05-23 19:57:23

+0

在eclipse中沒有SD卡,沒有文本文件,這意味着assignArr0是空的,所以我不能在eclipse中測試它。 在我的手機上,assignArr00已填充,這是肯定的。在其他部分中檢查Toast味精。它的大小當然是1。 – erdomester 2011-05-23 20:55:05

回答

0

發現解決方案:我不得不重新聲明lv1 = (ListView) findViewById(R.id.ListView01);我不是這個背後的原因,但花了我4個小時才發現這一點。

if (fileisempty == false) 
       { 
        adapter1.clear(); 
        adapter1.notifyDataSetChanged(); 
        lv1 = (ListView) findViewById(R.id.ListView01); 
        ArrayAdapter<String> adapter11; 
        adapter11 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00); 
        lv1.setAdapter(adapter11); 
        adapter11.notifyDataSetChanged(); 

       } 
       else 
       { 
        lv1 = (ListView) findViewById(R.id.ListView01); 
        ArrayAdapter<String> adapter11; 
        adapter11 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00); 
        lv1.setAdapter(adapter11); 
        adapter11.notifyDataSetChanged(); 
       }