0

I am new to Android. I have a CheckBox in My MainActivity and checkbox in the listview for selecting the items. Firstly I want to select all the checkbox of the Listview from Checkbox in the MainActivity. Secondly I am getting EmailId of the users in the Listview which I want them in my Gmail for sending the message. Thirdly I should have the option of sending the message to selected ones from the Listview. I am Posting My Code. I Hope my problem will be solved here.

User_Info.java Class:

public class UserInfo extends AppCompatActivity {

ListView listview;
Button btn_send;
CheckBox checkbox;
ArrayList<Getter_Setter> user_list;
UserInfoAdapter adapter;
ProgressDialog dialog;
Parser parser = new Parser();
String url = "http://........";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_info);

    listview = (ListView) findViewById(R.id.listview);
    checkbox = (CheckBox) findViewById(R.id.checkbox);
    btn_send = (Button) findViewById(R.id.btn_send);


    ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nf = cn.getActiveNetworkInfo();
    if (nf != null && nf.isConnected() == true) {

        new User_Info().execute();
    }

        btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Intent for Gmail here with all Email ID's
        }
    });
}

public class User_Info extends AsyncTask <String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(UserInfo.this);
        dialog.setIndeterminate(false);
        dialog.setMessage("Please Wait....");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        try {

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            JSONObject json = parser.getJSONFromUrl(url,nameValuePairs);
            user_list = new ArrayList<Getter_Setter>();

            int result = json.getInt("udata");

            JSONArray array = json.getJSONArray("result");

            if (result == 1){
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    Getter_Setter getter_setter = new Getter_Setter();
                    getter_setter.setId(jsonObject.getString("id"));
                    getter_setter.setName(jsonObject.getString("name"));
                    getter_setter.setEmail(jsonObject.getString("email"));
                    getter_setter.setPassword(jsonObject.getString("password"));
                    getter_setter.setReg_Date(jsonObject.getString("reg_date"));

                    user_list.add(getter_setter);
                    adapter = new UserInfoAdapter(UserInfo.this,user_list);
                }

            } if (result == 0){
                return "Something Went Wrong";
            }
         } catch (JSONException e) {
            e.printStackTrace();
            Log.e("Exception", "" + e.toString());
            return "Try Again";
        }
        return null;
    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();
        listview.setAdapter(adapter);
    }
}

UserInfoAdapter.java Class:

public class UserInfoAdapter extends BaseAdapter {

Context context;
ArrayList<Getter_Setter> user_info_list;


public UserInfoAdapter(Context mContext, ArrayList<Getter_Setter> m_user_info_list) {

    this.context = mContext;
    this.user_info_list = m_user_info_list;
}

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

@Override
public Object getItem(int position) {
    return user_info_list.get(position);
}

@Override
public long getItemId(int position) {
    return user_info_list.indexOf(position);
}

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

    final ViewHolder mHolder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.user_info_adapter, null);
        mHolder = new ViewHolder();

        mHolder.Name = (TextView) convertView.findViewById(R.id.txt_name);
        mHolder.Email = (TextView) convertView.findViewById(R.id.txt_email);
        mHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);

        mHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        int getPosition = (Integer) buttonView.getTag();

                        user_info_list.get(getPosition).setSelected((buttonView.isChecked()));
                    }
                });

        convertView.setTag(mHolder);
        convertView.setTag(R.id.txt_name, mHolder.Name);
        convertView.setTag(R.id.txt_email, mHolder.Email);
        convertView.setTag(R.id.checkbox, mHolder.checkbox);

    } else {
        mHolder = (ViewHolder) convertView.getTag();
    }
            mHolder.checkbox.setTag(position);
            mHolder.Name.setText("Name: " + user_info_list.get(position).getName());
            mHolder.Email.setText("Email: " + user_info_list.get(position).getEmail());
            mHolder.checkbox.setChecked(user_info_list.get(position).isSelected());


    return convertView;
    }

private class ViewHolder {
    private TextView Name;
    private TextView Email;
    private CheckBox checkbox;

 }

}

Getter_Setter.java Class:

public class Getter_Setter {

private String Id;
private String Name;
private String Email;
private String Password;
private String Reg_Date;

public boolean isSelected() {
    return Selected;
}

public void setSelected(boolean selected) {
    Selected = selected;
}

private boolean Selected = false;


public String getId() {
    return Id;
}

public void setId(String id) {
    Id = id;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public String getReg_Date() {
    return Reg_Date;
}

public void setReg_Date(String reg_Date) {
    Reg_Date = reg_Date;
}
  • You want to send mails to selected ones from listView – JIGAR Dec 15 '15 at 10:18
  • Why are you overwriting the `convertView` `tag`? Normally, you should only set it once, like `convertView.setTag(mHolder);`. Do you get any errors, and what exactly is not working in your code? – yennsarah Dec 15 '15 at 10:20
  • yes...and also want to select all checkbox of the listview from the checkbox of the Main Class.. –  Dec 15 '15 at 10:20
  • @Amy I am not getting any error...all is working file till I work....now I want to send the mail to the email I am getting from server through checkbox.. –  Dec 15 '15 at 10:22

2 Answers2

3

For selecting all the checkboxes inside activity

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                         for(int i=0;i< user_list.size();i++){
                            user_list.get(i).setSelected(
                                     checkbox.isChecked());
                         }
                        adapter.notifyDatasetChanged();
                    }
                });

change your adapter getview to

 mHolder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
 mHolder.checkbox.setChecked( user_info_list.get(getPosition).getSelected());

add getSelected() getter to your bean class

by doing this you can only select-deselect all listview check-boxes now to send selected.

Now, to send selected email, add this to your adapter class to

    private ArrayList<String> user_selected_list; 

    public UserInfoAdapter(Context mContext, ArrayList<Getter_Setter> m_user_info_list) {

        this.context = mContext;
        this.user_info_list = m_user_info_list;
        this.user_selected_list = new ArrayList<>();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      //your code as it is



        mHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                int getPosition = (Integer) buttonView.getTag();
Log.e("test","inside check...position"+getPosition);
                                user_info_list.get(getPosition).
                                   setSelected((buttonView.isChecked()));
                               if(isChecked){
                                    user_selected_list.add(user_info_list.get(getPosition).
                                                               getEmail());
                         Log.e("test","inserted..."+user_selected_list.size());
                               }else{
                                  Log.e("test","removed..."+user_selected_list.size());
user_selected_list.remove(user_info_list.get(getPosition).
                                                               getEmail());
                              }
                            }
                        });
        }

        public ArrayList<String> getSelected(){
         return user_selected_list;
        }

inside activity button send click event

    btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Intent for Gmail here with all Email ID's
               ArrayList<String> emails = adapter.getSelected();

             Log.e("test","....size"+emails.size());
              for(int i=0;i<emails.size();i++){
               //send mail with these selected emails
               Toast.makeText(UserInfo.this,emails.get(i),
                      Toast.LENGTH_SHORT).show();
              }

            }
H Raval
  • 1,903
  • 17
  • 39
  • Your Code is working for me but I confused with the changes you suggest me in the adapter class and how can i get the item of the selected checkbox in the string to send them in Intent. –  Dec 15 '15 at 10:47
  • getting error in .get(i). "cannot resolve symbol i" and in method getSelected() in adapter class. –  Dec 15 '15 at 10:58
  • got it but for the method "public ArrayList getSelected(){ user_selected_list }" any solution –  Dec 15 '15 at 11:01
  • sorry i just forgot to write return statement...see edits – H Raval Dec 15 '15 at 11:03
  • I have tested the items i selected any put in the toast but it's showing[]....Toast.makeText(UserInfo.this,emails+"",Toast.LENGTH_SHORT).show(); –  Dec 15 '15 at 11:09
  • you have to loop through list to get all emails...i thought you have basic coading idea...so i havent did that...see my edits now – H Raval Dec 15 '15 at 11:11
  • try to print log as per my edit....are you selecting any checkbox before clicking button? – H Raval Dec 15 '15 at 11:23
  • of course first I will select the checkbox then I will click the button....without selecting any checkbox will it mean anything... –  Dec 15 '15 at 11:29
  • ok....well i have tested code with my data and works perfect...have you put log? are getting size in log – H Raval Dec 15 '15 at 11:33
  • its impossible...at least 0 should be print in your logcat...check it properly – H Raval Dec 15 '15 at 11:34
  • Nothing is adding in the arraylist getting 0 in the logcat means data is not inserted in the arraylist used in the adapter –  Dec 16 '15 at 04:16
  • i have logged inside adapter....see my edit and tell me what is output over there – H Raval Dec 16 '15 at 04:50
  • i have edited my code...please go through it and i have put log inside adapter...where check box selection made...what is output when you select a checkbox...let me know – H Raval Dec 16 '15 at 05:04
  • Log declare inside the adapter is not showing in the logcat but Log used not button click is showing in the logcat...why?? –  Dec 16 '15 at 05:21
  • i cant understand what do you want to say....just copy paste whole my adapter class and let me know how many logs can you see – H Raval Dec 16 '15 at 06:47
  • Now it's showing the mail id's in toast but unfortunately when I select the option all it is showing only few and after that when i select single checkbox it is showing the first listview item instead of the checked one. –  Dec 16 '15 at 07:35
  • When I am selecting all then it's showing the mail id which are visible to me in the device means the items scrolled up and items below is not showing –  Dec 16 '15 at 07:41
  • can you please mail me your code at hetal@elsner.com...i ll check it out – H Raval Dec 16 '15 at 07:49
  • please send me whole package....how can i find your drawables and colors....and methods like getJSONFromUrl....mail me zip of whole – H Raval Dec 16 '15 at 09:11
  • It's Misbehaving when we select all and unselect and then select few from the list....and when we select all then the items which are visible to me on screen shows in the toast and rest doesn't.. –  Dec 16 '15 at 10:17
  • and I more thing I want take all selected items in my mailbox –  Dec 16 '15 at 10:18
  • i have sent you updated code...it is working...let me know if you want anything else in code – H Raval Dec 16 '15 at 10:19
  • Unable Send Mail To Selected Users:- for (int i = 0; i < user_list.size(); i++) { //send mail with these selected emails if (user_list.get(i).isSelected()) { String item = user_list.get(i).getEmail();; Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, item); startActivity(Intent.createChooser(emailIntent, "Send mail...")); –  Dec 16 '15 at 11:54
  • refer this http://stackoverflow.com/questions/14496687/how-to-send-email-to-multiple-recepients-in-android – H Raval Dec 16 '15 at 11:58
0

you can add a method which returns the whole dataset from the adapter that is

public Getter_Setter getDataSet()
{
return user_info_list;
}

on clicking the checkbox you can set the all the item is the list as selected and notify the data set changed

 foreach( Getter_Setter  getSet user_info_list){

    getSet.setSelected=true;
    adapter.notifyDatasetChanged();
    }

now you get the all selected the user from the list by adding a new method to filer the selected objects from the list

and one more thing you need to change in the adapter code

you need to check the object is selected or not in view binding

anoopg87
  • 82
  • 6