Listview从共享首选项添加重复项

时间:2016-03-29 21:27:22

标签: java android loops sharedpreferences controls

我有一个应用程序将一些数据存储在共享首选项中,然后对该数据运行一些逻辑控制。然后将数据写入listview(填充在另一个类中)。似乎我的listview将数据填充两次,即使它不应该。以下是我认为存在问题的代码:

final List<String> myIngredientsList = new ArrayList<String>(); //empty arraylist
final String arrayString = sharedPref.getString("myIngredients", null);
    if (arrayString == null) {
       //do nothing if empty
    } else {
        myIngredientsList.add(arrayString);

    }
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String value = lv.getItemAtPosition(position).toString();

            //check if arraylist contains item selected
            if (myIngredientsList.contains(value)) {
             //do nothing
            } else {
                //add value to arraylist


                myIngredientsList.add(value);


                //storeIngredients
                editor.putString("myIngredients", myIngredientsList.toString());
                editor.commit();


                Toast.makeText(getActivity().getApplicationContext(), value + " Added!", Toast.LENGTH_LONG).show();
            }


        }
    });

我最初在这里设置数据     onItemclicklistener,将数据存储为共享首选项,然后我在第二种方法中接收,操作日期,然后当用户返回此main方法时,我调用数据并检查是否存在重复条目。在我的脑海里,这应该工作。但它似乎忽略了重复的事实。任何见解都会有所帮助......

第二种方法填充了listview:

public class LiquorCabinetFrag extends Fragment {
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    return inflater.inflate(R.layout.liquor_cabinet_frag, parent, false);
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // Setup any handles to view objects here


    //initialize shared prefs
    final SharedPreferences sharedPref = LiquorCabinetFrag.this.getActivity().getPreferences(Activity.MODE_PRIVATE);
    final SharedPreferences.Editor editor = LiquorCabinetFrag.this.getActivity().getPreferences(Activity.MODE_PRIVATE).edit();
    //recall stored ingredients
    String arrayString = sharedPref.getString("myIngredients", null);

    if(arrayString ==null){
       //do nothing
    }else {
        //format and convert string
        String str1 = arrayString.replace("[", "");
        String str2 = str1.replace("]", "");
        String[] strValues = str2.split(",");
         final ArrayList<String> aListNumbers = new ArrayList<String>(Arrays.asList(strValues));

        //set listview
        lv = (ListView) getActivity().findViewById(R.id.liquor_cabinet_list_view);
        adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, aListNumbers);
        lv.setAdapter(adapter);



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                String value = lv.getItemAtPosition(position).toString();
                adapter.remove(value);
                adapter.notifyDataSetChanged();

                if (aListNumbers.isEmpty()) {
                    editor.putString("myIngredients", null);
                    editor.commit();
                }else {
                    //update
                    editor.putString("myIngredients", aListNumbers.toString());
                    editor.commit();
                }
            }
        });


    }

0 个答案:

没有答案