我有3个片段。
第一个片段用于显示Listview。
第二个片段是显示用户在第一个片段中点击了什么,然后按一个按钮(添加到购物车)。
第3个片段用于显示添加到购物车的产品总数。
我添加了多次不同的产品,但是,它只显示添加到购物车的最后一项,其余产品未显示在列表视图中
display_listview.java
public class display_listview extends Fragment {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_display_listview, container, false);
ListView listView = (ListView)v.findViewById(R.id.lv);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mobileArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String product_name=adapterView.getItemAtPosition(i).toString();
product_details my_alert=new product_details();
my_alert.show(getActivity().getSupportFragmentManager(),"");
Bundle bundle = new Bundle();
bundle.putString("name",product_name);
my_alert.setArguments(bundle);
}
});
Button get_button=(Button)v.findViewById(R.id.cart);
get_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getActivity().getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
my_cart list=new my_cart();
transaction.replace(R.id.top,list);
transaction.addToBackStack("wtf");
transaction.commit();
}
});
return v;
}
}
show_product.java
public class show_product extends DialogFragment {
LayoutInflater inflater;
View v;
ArrayList<String> products_clicked=new ArrayList<String>();
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater=getActivity().getLayoutInflater();
v=inflater.inflate(R.layout.fragment_product_details,null);
TextView get_text=(TextView)v.findViewById(R.id.text);
Bundle bundle = getArguments();
final String name= bundle.getString("name");
get_text.setText(name);
AlertDialog.Builder build=new AlertDialog.Builder(getActivity());
build.setView(v).setPositiveButton("Add to cart", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
SharedPreferences preferences = getActivity().getSharedPreferences("order_list", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
products_clicked.add(name);
Set<String> set = new HashSet<String>();
set.addAll(products_clicked);
editor.putStringSet("yourKey", set);
editor.commit();
Toast.makeText(getActivity(),name +" has added to cart.", Toast.LENGTH_LONG).show();
}
});
return build.create();
}
}
my_cart.java
public class my_cart extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_my_cart, container, false);
SharedPreferences preferences = getActivity().getSharedPreferences("order_list", Context.MODE_PRIVATE);
Set<String> set = preferences.getStringSet("yourKey", null);
List<String> sample=new ArrayList<String>(set);
ArrayAdapter adapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, sample);
ListView listView1 = (ListView)v.findViewById(R.id.listView);
listView1.setAdapter(adapter1);
return v;
}
}
请参阅截图
答案 0 :(得分:2)
这是因为product_clicked
是一个实例变量,并不保留以前添加的项目。
修改onClick()
类中的show_product
方法,如下所示:
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
SharedPreferences preferences = getActivity().getSharedPreferences("order_list", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
Set<String> set = preferences.getStringSet("yourKey", new HashSet<String>());
set.add(name);
editor.putStringSet("yourKey", set);
editor.commit();
Toast.makeText(getActivity(),name +" has added to cart.", Toast.LENGTH_LONG).show();
}
如您所见,我们在SharedPreferences
中获取以前保存的集,添加新项并再次存储。