2014-04-17 80 views
0

我有一個包含listview的活動。 listView有一個自定義適配器,因爲它包含兩個textViews和一個checkBox。現在我希望每次調用時,我想檢查所有複選框狀態並將它們保存到ArrayList<String>(好吧,我知道如何解析內容)。我知道我可以將它從getView()保存,但我不知道如何「在保存模式下重新加載listView」。如何將共享首選項的複選框狀態保存在列表視圖中

代碼:

adapter.class:

public class adapter extends ArrayAdapter<String> { 
LayoutInflater mInflater; 
List<String> text, quantity; 
boolean saving = false; 
String cbListName; 
String[] cbArray; 
ArrayList<String> cbList; 
Context context; 

public adapter(Context context, int resource, int textViewResourceId, List<String> objects, List<String> quantities, String arrayName) { 
    super(context, resource, textViewResourceId, objects); 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    text = objects; 
    quantity = quantities; 
    cbListName = arrayName + "_cbs"; 
    this.context = context; 
} 

public View getView(int position, View convertView, ViewGroup parent){ 
    View row = mInflater.inflate(R.layout.items_list_element, null); 

    CheckBox cb = (CheckBox)row.findViewById(R.id.gotItCheckBox); 
    TextView itemName = (TextView)row.findViewById(R.id.text1); 
    TextView itemQuantity = (TextView)row.findViewById(R.id.quantityText); 

    //Populate text fields 
    if (text.size() > 0){ 
     itemName.setText(text.get(position)); 
     itemQuantity.setText(quantity.get(position)); 
    } 

    loadArray(); 

    //Sets checkbox states 
    /*if (cbList.size() > 0 & cbList.get(position).equals("true")){ 
     cb.setChecked(true); <-- This is in comment because throws exceptions otherwise 
    }*/ 

    //Save checkbox values if saving is true 
    if (saving){ 
     if (cb.isChecked()){ 
      cbList.add(position, "true"); 
     }else { 
      cbList.add(position, "false"); 
     } 
     saving = false; 
    } 

    return row; 
} 

private void loadArray(){ 
    cbArray = functions.loadArray(cbListName, context); 
    cbList = new ArrayList<String>(); 
    Collections.addAll(cbList, cbArray); 
} 

public void saveArray(){ 
    cbArray = new String[cbList.size()]; 
    cbArray = cbList.toArray(cbArray); 
    functions.saveArray(cbArray, cbListName, context); 
} 

}

活動與listView

public class ShoppingList extends MenuActivity { 
EditText itemNameField, itemQuantityField; 
Button addButton, plusButton, minusButton; 
ListView listView; 
int quantity; 
String [] itemArray, itemQuantityArray; 
ArrayList<String> itemList, itemQuantityList; 
adapter adapter; 
String saveArrayName; 
Context context = this; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shoppinglist); 
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

    //Sets up all the views from ids 
    setupViews(); 

    //Defines button click actions 
    plusButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      quantity = Integer.parseInt(itemQuantityField.getText().toString()); 
      quantity ++; 
      itemQuantityField.setText(String.valueOf(quantity)); 
     } 
    }); 
    minusButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      quantity = Integer.parseInt(itemQuantityField.getText().toString()); 
      quantity --; 
      if (quantity < 1){ 
       quantity = 1; 
      } 
      itemQuantityField.setText(String.valueOf(quantity)); 
     } 
    }); 
    addButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!(itemNameField.getText().toString().equals(""))){ 
       itemList.add(itemNameField.getText().toString()); 
       itemQuantityList.add(itemQuantityField.getText().toString()); 
       adapter.notifyDataSetChanged(); 
       itemNameField.setText(""); 
       itemQuantityField.setText("1"); 
       InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       mgr.hideSoftInputFromWindow(addButton.getWindowToken(), 0); 
      } 
     } 
    }); 

    //Calls the function to load the array 
    setupArray(); 

    adapter = new com.grizeldi.gShopper.adapter(this, R.layout.items_list_element, android.R.id.text1, itemList, itemQuantityList, saveArrayName); 
    listView.setAdapter(adapter); 
    listView.setCacheColorHint(0); 
} 

private void setupViews(){ 
    itemNameField = (EditText) findViewById(R.id.addItemField); 
    itemQuantityField = (EditText) findViewById(R.id.itemQuantityField); 
    addButton = (Button) findViewById(R.id.addItemButton); 
    plusButton = (Button) findViewById(R.id.plusButton); 
    minusButton = (Button) findViewById(R.id.minusButton); 
    listView = (ListView) findViewById(R.id.itemView); 
} 

private void setupArray(){ 
    Intent whoStartedIt = getIntent(); 
    saveArrayName = whoStartedIt.getStringExtra("arrayName"); 

    //Loads item names 
    itemArray = functions.loadArray(saveArrayName, this); 
    itemList = new ArrayList<String>(); 
    Collections.addAll(itemList, itemArray); 

    //Loads item quantities 
    itemQuantityArray = functions.loadArray(saveArrayName + "_quantity", this); 
    itemQuantityList = new ArrayList<String>(); 
    Collections.addAll(itemQuantityList, itemQuantityArray); 
} 

public void onPause(){ 
    super.onPause(); 

    //Saves item names 
    itemArray = new String[itemList.size()]; 
    itemArray = itemList.toArray(itemArray); 
    functions.saveArray(itemArray, saveArrayName, this); 

    //Saves item quantities 
    itemQuantityArray = new String[itemQuantityList.size()]; 
    itemQuantityArray = itemQuantityList.toArray(itemQuantityArray); 
    functions.saveArray(itemQuantityArray, saveArrayName + "_quantity", this); 

    /* 
    adapter.notifyDataSetChanged(); <-- I want to save array here 
    adapter.saveArray();*/ 
} 
} 

對不起,如果我的問題是太混亂了:P

有沒有人知道這裏發生了什麼?我需要儘可能快的答案。

編輯:MenuActivity只是一個類,它擴展活動,並彈出菜單時單擊菜單按鈕。

回答

0

我認爲你不應該使用共享首選項。試試這個。 保留一個mysqlitedatabase表,它可以存儲複選框的值,即1表示選中,0表示未選中。 單擊某個複選框時,調用其偵聽器並更改數據庫中的值。如果你擔心加工使用LoaderManager

+0

嗯...監聽器是我現在想要使用的;)但是可能我不會使用SQL,因爲我不知道如何使用它,而另一方面我知道相當不錯使用共享首選項。我還有一個將字符串數組保存到sharedprefs的方法。 – grizeldi

0

好吧,我想出瞭如何解決這個問題。我使用了一個onCheckedChangeListener我操縱數組值。

相關問題