我有一个包含listview的活动。 listView
有一个自定义适配器,因为它包含两个textViews
和一个checkBox
。现在我希望每次调用该活动onPause
时,我想检查所有复选框状态并将它们保存到ArrayList<String>
(好吧我知道如何解析东西)。我知道我可以从getView()
保存,但我不知道如何在保存模式下重新加载listView
&#34;。
代码:
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 :(得分:0)
在我看来,你不应该使用共享偏好。试试这个。 保留一个mysqlitedatabase表,可以存储复选框值,即1表示已选中,0表示未选中。 单击checkBox时,调用其侦听器并更改数据库中的值。如果您担心处理使用LoaderManager类
答案 1 :(得分:0)
好的,我想出了如何解决这个问题。
我使用onCheckedChangeListener
来操作数组值。