0

I have a ListView in my Fragment and I want to have one button like "Get checked items" that checks what items have been clicked. Previously I used setOnItemClickListener,and it showed what item is clicked right now, but it seems as for button I need to use OnClick.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.feat,container,false);
     ListView list=(ListView)view.findViewById(R.id.listView1);
    final Button button=(Button)view.findViewById(R.id.button1);
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    list.setItemChecked(4, true);

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_multiple_choice,items);
    list.setAdapter(adapter);
    button.setOnClickListener(this);

    return view ;
}

@Override
public void onClick(View v) {
    int postition=(Integer)v.getTag();
    //What should be here?
}
Vlad Demyan
  • 363
  • 1
  • 4
  • 16
  • You can get the checked items using `list.getCheckedItemPositions()`. Check out this answer: http://stackoverflow.com/a/19414593/1240523 – RobVoisey Apr 11 '16 at 14:24
  • when adding that piece of code into OnClick, it can't resolve list. While if I add that into OnCreateView it won't work with the click of the button – Vlad Demyan Apr 11 '16 at 14:32
  • I can't help with programming principles, but your list needs to be put into the namepsace of the activity, – RobVoisey Apr 11 '16 at 14:34
  • Thanks, the link you posted worked for me, but it's working only when I click on the item, and I don't know how to use it with OnClick method – Vlad Demyan Apr 11 '16 at 14:53

1 Answers1

0

You could simply store the positions of checked items in a global int array and check the array on Button click. Maybe not the smartest but looks like fastest solution to me?

Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27