mardi 11 août 2015

Android - Listview with checkbox

I have implemented a listview with checkbox where the checkbox is ticked whenever the user clicks on it. But I realized it's pretty inconvenient this way and I'd like to change it to whenever the user clicks on a row, the checkbox will be automatically ticked. My question is how can I do that? How can I change the code in listView.setOnItemClickListener such that my checkbox will be ticked? Any ideas?

This is my code so far:

MyCustomAdapter dataAdapter = null;
    public static ArrayList countries = new ArrayList();;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isNetworkAvailable()) {
            setContentView(R.layout.activity_show_countries);

            ArrayList countries = new ArrayList();
            countries.clear(); // refresh

            //Generate list View from ArrayList
            displayListView();

            Button btn1 = (Button) findViewById(R.id.next);

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    startActivity(new Intent(ShowCountries.this, ShowTypes.class));
                }
            });
        }
        else
        {
            startActivity(new Intent(ShowCountries.this, NoInternetConnection.class));
        }

    }

    private void displayListView() {

        //Array list of countries
        countries.clear();//refresh
        ArrayList<Country> countryList = new ArrayList<Country>();
        Country country = new Country("POIs","Show POIs around me",false);
        countryList.add(country);

        //create an ArrayAdaptar from the String Array
        dataAdapter = new MyCustomAdapter(this,
                R.layout.country_info, countryList);
        ListView listView = (ListView) findViewById(R.id.list_countries);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);


        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // When clicked, show a toast with the TextView text

                 Country country = (Country) parent.getItemAtPosition(position);
            if (countries.indexOf(country.getName()) !=-1) {
                countries.remove(country.getName());
            }
            else
            {
                // thick checkbox
                countries.add(country.getName());
            }
              //  Toast.makeText(getApplicationContext(),
              //          "Clicked on Row: " + country.getName(),
             //           Toast.LENGTH_LONG).show();
            }
        });

    }

    private class MyCustomAdapter extends ArrayAdapter<Country> {

        private ArrayList<Country> countryList;

        public MyCustomAdapter(Context context, int textViewResourceId,
                               ArrayList<Country> countryList) {
            super(context, textViewResourceId, countryList);
            this.countryList = new ArrayList<Country>();
            this.countryList.addAll(countryList);
        }

        private class ViewHolder {
            TextView code;
            CheckBox name;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));

            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.country_info, null);

                holder = new ViewHolder();
                holder.code = (TextView) convertView.findViewById(R.id.code);
                holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
                convertView.setTag(holder);

                holder.name.setOnClickListener( new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v ;
                        Country country = (Country) cb.getTag();
                        Toast.makeText(getApplicationContext(),
                                "Clicked on Checkbox: " + cb.getText() +
                                        " is " + cb.isChecked(),
                                Toast.LENGTH_LONG).show();
                        if (countries.indexOf(cb.getText()) !=-1) {
                            country.setSelected(cb.isChecked());
                            countries.remove(cb.getText());
                        }
                        else
                        {
                            countries.add(cb.getText());
                        }
                    }
                });
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            Country country = countryList.get(position);
            holder.code.setText(" (" +  country.getCode() + ")");
            holder.name.setText(country.getName());
            holder.name.setChecked(country.isSelected());
            holder.name.setTag(country);

            return convertView;
        }

    }
}




Aucun commentaire:

Enregistrer un commentaire