这是我的原始ListView类,我从列表中选择一个项目,但我想选择相同的项目4次,然后在另一个活动中它应该添加项目4次。我可以在另一个活动中添加一次。我使用了共享偏好来添加。任何人都可以帮助我如何存储4倍的价值?任何提示将不胜感激:((谢谢
//这是我选择项目的类
public class Alcohol extends Activity {
private List<Alcohols> alcohol = new ArrayList<Alcohols>();
String wine;
String beer;
SharedPreferences someData;
public static String filename = "MySharedString";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alcohol);
someData = getSharedPreferences(filename, 0);
AlcoholList();
AlcohollistView();
registerClickCallBack();
}
private void registerClickCallBack() {
// TODO Auto-generated method stub
ListView list = (ListView) findViewById(R.id.AlcoholListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
switch (position) {
case 0:
wine = "wine";
SharedPreferences.Editor editor0 = someData.edit();
editor0.putString("sharedString4", wine);
editor0.commit();
otherActivity("list");
break;
case 1:
beer = "beer";
SharedPreferences.Editor editor1 = someData.edit();
editor1.putString("sharedString5", beer);
editor1.commit();
otherActivity("list");
break;
}
});
}
private void AlcohollistView() {
// TODO Auto-generated method stub
ArrayAdapter<Alcohol> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.AlcoholListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Pilstyper> {
public MyListAdapter() {
super(Alcohol.this, R.layout.list_item_view, alcohol);
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(
R.layout.activity_item_view, parent, false);
}
Alcohol alcohol = alcohol.get(position);
ImageView imageView = (ImageView) itemView
.findViewById(R.id.wine);
imageView.setImageResource(alcohol.getIkonId());
// Text
TextView text = (TextView) itemView.findViewById(R.id.text_wine);
text.setText(alcohol.getText());
return itemView;
}
}
private void AlcoholList() {
// TODO Auto-generated method stub
alcohol.add(new Alcohol("Wine", R.drawable.wine));
alcohol.add(new Alcohol("Beer", R.drawable.beer));
}
public void otherActivity(String a) {
Intent i = new Intent();
if (a.equals("list")) {
i.setClass(getApplicationContext(), list.class);
}
startActivity(i);
finish();
}
}
这是另一个listview类,如果我在上一课中选择了四次,我想要添加一个项目4次。我只能添加一次://
public class list extends Activity {
private List<Alcohol> alcohol = new ArrayList<Alcohol>();
ArrayAdapter<Alcohol> adapter;
SharedPreferences someData;
public static String filename = "MySharedString";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
someData = getSharedPreferences(filename, 0);
String wine = someData.getString("sharedString4", "ooo");
String beer = someData.getString("sharedString5", "ooo");
if(wine.equals("wine")){
alcohol.add(new Alcohol("Wine", R.drawable.wine));
}else if(wine.equals("wine")){
alcohol.add(new Alcohol("Beer", R.drawable.beer));
}
}
答案 0 :(得分:0)
在onItemClick()中,只存储要传递给数组列表中其他活动的值。像list.add()然后移动到其他活动与一个提交类型的公共按钮。可以放在列表视图之后。并有意地传递该列表。
答案 1 :(得分:0)
如果它类似于添加到购物车类型,则将每个选项保存到您之前的活动可访问的表格或对象中,并将其加载到onResume或onStart。