0

I want to manage a list in a listView using onLongClick. I want to select multiple items and manage (when is selected) adding in the toolbar 1 botton (remove). I try with this code:

public class FragmentFragment extends Fragment {
    private ListView listView;
    private List<Schede> list;
    private SchedeAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
        FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SecondActivity.class);
                startActivity(intent);
            }
        });
        listView = (ListView) rootView.findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object object= list.get(position);
                Toast.makeText(getContext(), object.getName() + "Clicked", Toast.LENGTH_SHORT).show();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                Object object = list.get(position);
                view.setSelected(true);
                return false;
            }
        });
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        return rootView;
    }

    public void remove(int position){
        SharedPreferences pref = getActivity().getSharedPreferences("OBJECTS", Context.MODE_PRIVATE);
        list.remove(position);
        String listS = new Gson().toJson(list);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString("OBJECTS", listS);
        edit.apply();
        adapter.notifyDataSetChanged();    
    }    

    public void refresh(){
        SharedPreferences pref = getActivity().getSharedPreferences("OBJECTS", Context.MODE_PRIVATE);
        String string = pref.getString("OBJECTS", null);
        if (string != null){
            Type type = new TypeToken<List<Object>>(){}.getType();
            list = new Gson().fromJson(string, type);
            adapter = new ObjectAdapter(getContext(), list);
            listView.setAdapter(adapter);
        }
    }

    @Override
    public void onResume(){
        super.onResume();
        refresh();
    }    
}

...but don't allow me to select multiple items (I set different background color for selected items in xml). The rest of code work correctly

Edit: post an example Item (chat) selected with button in top (archive)

ArK
  • 20,698
  • 67
  • 109
  • 136
Pablito
  • 47
  • 1
  • 7

1 Answers1

0

Resolved: https://stackoverflow.com/a/12598337/6941150 Using in onCreate()...

listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
...
}
Community
  • 1
  • 1
Pablito
  • 47
  • 1
  • 7