2016-08-07 51 views
1

我有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; 
} 
} 

請參閱截圖

enter image description here

+0

您確定要在display_listview類中調用show_product嗎?我只能看到product_details用於顯示警報? – fluffyBatman

+0

對於不同的主題,java類的名稱必須以大寫字母開頭。 – fluffyBatman

+0

我沒有得到你的意思,但到目前爲止,我的編碼是好​​的,除了我上面提到的問題 – gosulove

回答

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設置,添加新項目,再存放起來。

+0

thx!在改變你的建議之後,它會起作用,但另一個問題出現了。如果我關閉應用程序並重新啓動,當我第一次查看「我的購物車」時,它總是顯示「WindowsMo​​bile」的第一項(我沒有點擊添加到購物車)。任何想法發生了什麼? – gosulove

+0

'SharedPreferences'永久保存數據。它在那裏,因爲在之前的發佈中,您已將其添加到購物車。這不是你想要的嗎?如果你不想爲下次啓動存儲數據,那麼根本不要使用'SharedPreferences'。使用您以前的代碼,但使'products_clicked'靜態。 – Mousa

+0

好的。順便說一句,只是想知道我是否使用了購物車的正確方法?我想通過使用SharedPreferences將項目保存在購物車中。 – gosulove