2012-08-05 84 views
0

我是Android設備編程的新手。我想弄清楚如何將複選框保存在使用simple_list_item_multiple_choice和數組適配器生成的listview中。我希望能夠保存複選框的狀態,以便當用戶點擊後退按鈕轉到另一個清單時,他們可以返回到此清單並在他們離開時拾取。使用陣列適配器保存列表視圖simple_list_item_multiple_choice複選框狀態

請請,請幫助!代碼和/或解釋將是理想的。

代碼:

public class BeachBabyStuff extends Activity { 
    String[] beachstuffbaby = new String[]{                 
         "Beach Blanket or Mat",            
      "Beach Towels",                  
      "Beach Umbrella",                 
      "Beach Chair",                   
      "Books/Magazines",                
      "Radio",                  
      "Pen/Paper",                
      "Tablet"};                  


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listview); 


     // Getting the reference to the listview object of the layout 
     ListView listView = (ListView) findViewById(R.id.listview); 

     // The checkbox for the each item is specified by the layout android.R.layout.simple_list_item_multiple_choice 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, beachstuffbaby); 


     // Setting adapter to the listview 
     listView.setAdapter(adapter); 

     //Manage the onItemClick method 
     listView.setOnItemClickListener(new OnItemClickListener() { 

      private View view; 

      public void onItemClick(AdapterView<?> ListView, View view, int position, long id) { 
         CheckedTextView textView = (CheckedTextView)view; 
         textView.setChecked(!textView.isChecked()); 

         this.view = view; 

            } 

     }); 
+0

什麼是當前行爲.. AFAIK各國不應該改變從其他活動返回.. – Ronnie 2012-08-05 11:16:08

回答

0

添加一個布爾值數組您activty維護的檢查。

boolean checked[] = new boolean[beachstuffbaby.length]; 

將所有檢查的位置設置到此數組中。從該數組中製作一個逗號分隔的項目字符串並放入SharedPreferences

通過從首選項中讀取此數組並恢復逗號分隔字符串來恢復狀態。

+0

謝謝。我會嘗試發佈我的結果。希望這也能幫助別人。 – user1574481 2012-08-05 16:23:14

+0

我是否需要添加自定義適配器? – user1574481 2012-08-06 18:01:01

相關問題