vendredi 15 septembre 2017

how do I toast the checkboxes in my ListView on button click

I've been following this post on how to toast checkboxes that have been checked in my ListView when a button is clicked : using checkbox in android for displaying toast message in listview but I'm having a problem in my NewContact activity - the one where the button is, for clicking, and it will toast the checked items.

The problem is I'm missing a parameter in my onPostExecute, in the place of the ???? :

      @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);

                adapter = new SelectPhoneContactAdapter(selectPhoneContacts, NewContact.this, ????);

                adapter.notifyDataSetChanged();

                listView.setAdapter(adapter);

}

I've tried stuff, theContactsList...so many of the variables from my Custom Adapter that I thought would do it, but not sure what it is.

I want to toast the checked values with this code, in my NewContact Activity:

showtoast = (Button) findViewById(R.id.showtoast);

        showtoast.setOnClickListener(new View.OnClickListener() {

            StringBuilder result = new StringBuilder();
            for(int i=0;i<arraylist.size();i++)
            {
                if(adapter.mCheckStates.get(i)==true)
                {

                    result.append(arrayList.get(i).get(MainActivtiy.Name));
                    result.append("\n");
                }

            }
            Toast.makeText(ActivityName.this, result, 1000).show();

        }

Everything was working fine, the NewContact activity and my Custom Adapter, until I started trying to add the checkboxes, but I really need the checkboxes so have to keep going. I've got my custom adapter set up - at least, no errors or anything. Here's the code :

SelectPhoneContactsAdapter:

public class SelectPhoneContactAdapter extends BaseAdapter implements android.widget.CompoundButton.OnCheckedChangeListener {

    //define a list made out of SelectPhoneContacts and call it theContactsList
    public List<SelectPhoneContact> theContactsList;
    //define an array list made out of SelectContacts and call it arraylist
    private ArrayList<SelectPhoneContact> arraylist;

    Context context;
    ArrayList<HashMap<String, String>> stuff;
    HashMap<String, String> resultp = new HashMap<String, String>();
    SparseBooleanArray mCheckStates;

    //define a ViewHolder to hold our name and number info, instead of constantly querying
    // findviewbyid. Makes the ListView run smoother
    ViewHolder v;

    public SelectPhoneContactAdapter(final List<SelectPhoneContact> selectPhoneContacts, Context context,
                                     ArrayList<HashMap<String, String>> arraylistofstuff) {

        theContactsList = selectPhoneContacts;
        this.context = context;
        this.arraylist = new ArrayList<SelectPhoneContact>();
        this.arraylist.addAll(theContactsList);
        stuff = arraylistofstuff;
        mCheckStates = new SparseBooleanArray(stuff.size());

    }


    @Override
    public int getCount() {
        return stuff.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

    static class ViewHolder {
        //        In each cell in the listview show the items you want to have
//        Having a ViewHolder caches our ids, instead of having to call and load each one again and again
        TextView title, phone;
        CheckBox check;
    }

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

        //we're naming our convertView as view
        View view = convertView;


        if (view == null) {

            v = new ViewHolder();


            //if there is nothing there (if it's null) inflate the view with the layout
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = li.inflate(R.layout.phone_inflate_listview, null);

            // Get the position
            resultp = stuff.get(i);

            //      So, for example, title is cast to the name id, in phone_inflate_listview,
//        phone is cast to the id called no etc
            v.title = (TextView) view.findViewById(R.id.name);
            v.phone = (TextView) view.findViewById(R.id.no);
            v.check = (CheckBox) view.findViewById(R.id.checkBoxContact);


            v.check.setTag(i);
            v.check.setChecked(mCheckStates.get(i, false));
            v.check.setOnCheckedChangeListener(this);


            view.setTag(v);

        } else {
            view = convertView;

        }


//        store the holder with the view
        final SelectPhoneContact data = (SelectPhoneContact) theContactsList.get(i);

        //in the listview for contacts, set the name
        v.title.setText(data.getName());
        //in the listview for contacts, set the number
        v.phone.setText(data.getPhone());

        // Return the completed view to render on screen
        return view;

    }

    public boolean isChecked(int i) {
        return mCheckStates.get(i, false);
    }

    public void setChecked(int i, boolean isChecked) {
        mCheckStates.put(i, isChecked);

    }

    public void toggle(int i) {
        setChecked(i, !isChecked(i));

    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {

        mCheckStates.put((Integer) buttonView.getTag(), isChecked);

    }


    }

Aucun commentaire:

Enregistrer un commentaire