【问题标题】:ArrayList only display 1 item in SharedPreferencesArrayList 仅在 SharedPreferences 中显示 1 项
【发布时间】:2016-12-13 07:48:43
【问题描述】:

我有 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 用于显示警报?
  • 换个话题,java类的名字必须以大写字母开头。
  • 我不明白你的意思,但到目前为止我的编码还可以,除了我上面提到的问题

标签: android android-fragments sharedpreferences


【解决方案1】:

这是因为product_clicked 是一个实例变量,不会保留您之前添加的项目。

像这样修改show_product类中的onClick()方法:

@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 中获取先前保存的集合,添加新项目并再次存储。

【讨论】:

  • 谢谢!更改为您的建议后,它可以工作,但又出现了另一个问题。如果我关闭应用程序并重新启动,当我第一次查看“我的购物车”时,它总是显示第一项的“WindowsMo​​bile”(我没有点击添加到购物车)。知道发生了什么吗?
  • SharedPreferences 永久保存数据。它在那里,因为在以前的发布中,您已将其添加到购物车。这不是你想要的吗?如果您不希望为下次启动存储数据,则根本不要使用SharedPreferences。使用您之前的代码,但将 products_clicked 设为静态。
  • 好的。顺便说一句,只是想知道我是否使用了正确的购物车方法?我想使用 SharedPreferences 将商品保存在购物车中。
  • 保存在 SharedPreferences 中是可以的。另一种解决方案是使用 sqlite 数据库。
猜你喜欢
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
相关资源
最近更新 更多