我有3個片段。ArrayList只顯示SharedPreferences中的1項
1st片段用於顯示Listview。
第二個片段是顯示用戶在第一個片段中點擊的按鈕(添加到購物車)。
第3個片段用於顯示要添加到購物車的產品總數。
我已經加入了不同的產品,很多次,但是,它只能顯示最後一個項目被添加到購物車,其餘的產品並沒有在ListView
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;
}
}
請參閱截圖
您確定要在display_listview類中調用show_product嗎?我只能看到product_details用於顯示警報? – fluffyBatman
對於不同的主題,java類的名稱必須以大寫字母開頭。 – fluffyBatman
我沒有得到你的意思,但到目前爲止,我的編碼是好的,除了我上面提到的問題 – gosulove