0
我有一個帶有項目水果,蔬菜,肉類的抽屜列表視圖。每個類都有一個擴展ListFragment的類。我還爲我的ArrayList和ArrayAdapter創建了一個Product類。但每次我切換ListFragment和切換回來,我所做的更改也重置,而不是保存。如何在使用抽屜切換ListFragment時保存ArrayList中的更改ListView
這裏是我當前的代碼:
product_adapter.java
//imports ....
public class product_adapter extends ArrayAdapter<product> {
public product_adapter(Context context, ArrayList<product> prod) {
super(context, 0, prod);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final product products = getItem(position);
// Some Codes for Instantiation and LayoutInflater
itemAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
products.itemQuantity ++;
viewHolder.itemQuantity.setText(String.valueOf(products.getItemQuantity()));
notifyDataSetChanged();
}
});
return convertView;
}
}
Fruit.java
public class Fruit extends ListFragment {
public ArrayList<product> products;
public product_adapter productAdapter;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
products = new ArrayList<product>();
products.add(new product("Apple", 18.00 /*Price*/, 0 /*Quantity*/));
products.add(new product("Orange", 16.00 /*Price*/, 0 /*Quantity*/));
productAdapter = new product_adapter(getActivity(),products);
setListAdapter(productAdapter);
}
}
蔬菜和肉類的構造方式爲水果類相同
與ListView的MainActivity
//Some codes here for the creating ListView
listView = (ListView) findViewById(R.id.categoryView);
listView.setAdapter(new category_adapter(this, categories));
drawerLayout = (DrawerLayout) findViewById(R.id.drawerView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
ListFragment listFragment = null;
switch (position){
case 0:
listFragment = new Fruit();
break;
case 1:
listFragment = new Veggies();
break;
case 2:
listFragment = new Meat();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.itemLayout,listFragment).commit();
drawerLayout.closeDrawers();
}
});
你的意思是在按下後退按鈕? – Moulesh
sry有關我忘了提及它是抽屜列表視圖 –
您需要在抽屜上展開列表來對您的產品進行分類? – Moulesh