lundi 6 novembre 2017

using setSelection(int position) to move items from listview to the top or bottom when checkbox is checked

I have a listview that contains a checkbox next to each item. I want to move items to the top or bottom of the list by checking or unchecking them. If I check the item, move to the top and I uncheck, move to the bottom. I have tried to implement setSelection(int position). I have been focusing on the if and else statements on the Adapter since they pertain to what happens when you check and uncheck the checkbox. When I do try to implement the setSelection(int position) method, I get error messages.

public class MainActivity extends AppCompatActivity {

ArrayList<String> selectedItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    selectedItems = new ArrayList<String>();
    ListView chl = (ListView) findViewById(R.id.cityselector);
    chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    String[] items = {"Berlin", "New York", "Paris", "Mexico City", "Asana", "Tangier"};
    Adapter adapter=new Adapter(this,items);
    chl.setAdapter(adapter);

    chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            int item_position=i;



        }
    });

}

Adapter Java Class

public class Adapter extends BaseAdapter {
    String[] items;
    LayoutInflater inflater;
   ArrayList selectedItems;
    Activity activity;
    Adapter(Activity activity, String[] items){

        this.items=items;
        inflater=activity.getLayoutInflater();
        selectedItems = new ArrayList<String>();
        this.activity=activity;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public String getItem(int i) {
        return items[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        View v = view;
        if(v == null){
            v = inflater.inflate(R.layout.checkboxlayout,null);
        }

        TextView textview_Cities=(TextView)v.findViewById(R.id.Textview_Cities);
        textview_Cities.setText(items[i]);

        final CheckedTextView  checkedTextView=(CheckedTextView)v.findViewById(R.id.checkbox);
        checkedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!checkedTextView.isChecked()){
                    checkedTextView.setChecked(true);
                    selectedItems.add(items[i]);
                    //This is where I put the setSelection(int position) method////         


                }
                else {
                    checkedTextView.setChecked(false);
                    selectedItems.remove(items[i]);
                }


            }
        });



        return v;
    }
}




Aucun commentaire:

Enregistrer un commentaire