我的购物车中有一个 ExpandableListView,我使用 ExpandableListAdapter 和购物车活动将商品加载到我的购物车中。当一个项目被添加到购物车时,我可以显示它,但是一旦我更改活动并返回购物车,购物车就会变成空的,它会将添加的项目显示为空。我怎样才能在我的购物车中保存价值。
我阅读了有关使用的信息onSaveInstanceState(Bundle)
,onRestoreInstanceState()
但我对如何准确使用它感到有些困惑。下面我展示了我的代码。谁能帮我这个。任何帮助将不胜感激。
public class ShoppingCartActivity extends Activity {
ProductAdapter mCartList;
ExpandableListView expListView;
List<String> listDataHeader;
List<String> listDataHeaderPrice;
List<String> listDataHeaderQty;
HashMap<String, List<String>> listDataChild;
private ExpandableListView mExpandableList;
String description;
String price;
String quantity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_activity);
quantity = getIntent().getStringExtra("quantity");
price = getIntent().getStringExtra("price");
description = getIntent().getStringExtra("description");
List<String> myList = new ArrayList<String>();
myList.add(description);
myList.add(quantity);
myList.add(price);
ActionBar actionBar = getActionBar();
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(
android.R.color.transparent)));
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
mCartList = new ProductAdapter(this, listDataHeader,
listDataHeaderPrice, listDataHeaderQty, listDataChild);
// setting list adapter
expListView.setAdapter(mCartList);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + listDataHeader.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataHeaderQty = new ArrayList<String>();
listDataHeaderPrice = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add(description);
listDataHeaderQty.add(quantity);
listDataHeaderPrice.add(price);
// Adding child data
List<String> description = new ArrayList<String>();
description.add("The Shawshank");
List<String> quantity = new ArrayList<String>();
quantity.add("The Shawshank");
List<String> price = new ArrayList<String>();
price.add("The Shawshank");
listDataChild.put(listDataHeader.get(0), description);
listDataChild.put(listDataHeaderQty.get(0), quantity);
listDataChild.put(listDataHeaderPrice.get(0), price);
// Header, Child
// data
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
mExpandableList = (ExpandableListView) findViewById(R.id.lvExp);
mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width
- GetPixelFromDips(10));
}
public int GetPixelFromDips(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
menu.getItem(1).setVisible(false);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
finish();
overridePendingTransition(R.anim.slide_in_left,
R.anim.slide_out_right);
return true;
case R.id.action_search:
Intent i = new Intent(getApplicationContext(),
SearchResultsActivity.class);
startActivityForResult(intent, 0); //added his
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
return true;
case R.id.action_cart:
Intent intent = new Intent(getApplicationContext(),
ShoppingCartActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
在我的第二个活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
setResult(RESULT_OK);
// get the action bar
ActionBar actionBar = getActionBar();
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
// Enabling Back navigation on Action Bar icon
actionBar.setDisplayHomeAsUpEnabled(true);
txtQuery = (TextView) findViewById(R.id.txtQuery);
handleIntent(getIntent());
}