选择All By One复选框并使用Intent获取Selected复选框的项目

时间:2015-12-15 10:16:26

标签: android listview checkbox

我是Android新手。我在My MainActivity中有一个CheckBox,在listview中有一个复选框用于选择项目。首先,我想从MainActivity中的Checkbox中选择Listview的所有复选框。其次,我正在获取Listview中用户的EmailId,我希望他们在我的Gmail中发送消息。第三,我应该可以选择将消息发送到Listview中的选定消息。我正在发布我的代码。我希望我的问题能在这里得到解决。

User_Info.java类:

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类:

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类:

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;
}

2 个答案:

答案 0 :(得分:3)

选择活动中的所有复选框

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();
                    }
                });

将适配器getview更改为

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

将getSelected()getter添加到bean类

通过执行此操作,您只能选择 - 现在取消选择所有列表视图复选框以发送所选。

现在,要发送所选的电子邮件,请将其添加到适配器类

    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();
              }

            }

答案 1 :(得分:0)

您可以添加一个从适配器返回整个数据集的方法  

public Getter_Setter getDataSet()
{
return user_info_list;
}
单击复选框后,您可以将所有项目设置为所选列表并通知数据集已更改

 foreach( Getter_Setter  getSet user_info_list){

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

现在,您可以通过添加新方法从列表中选择所选对象来从列表中选择所有用户

还需要在适配器代码中更改一件事

您需要检查视图绑定中是否选择了对象