lundi 14 décembre 2015

Select all fields with one checkbox(MainClass) and select multiple fields(AdapterClass) in Listview.

I am new to Android. I want to select all the items through one checkbox in the Main Class and select from the list both the options and send the info(Mail Id's) of selected items on button click. I am also facing a Strange problem when I am selecting items from the listview, others are selected automatically. How this happening Here I am Posting My Code. I Hope my problem will be solved here.

MainActivity:

public class MainActivity extends AppCompatActivity {

CheckBox checkbox;
ListView listview;
Button btn_send;
ProgressDialog dialog;
private static int count = 0;
private static boolean isNotAdded = true;
Parser parser = new Parser();
ArrayList<Getter_Setter> user_list;
UserInfoAdapter adapter;
SparseBooleanArray mChecked = new SparseBooleanArray();
String url = "http://........";

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

    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) {

        }
    });

    if (isNotAdded) {

        checkbox.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                for (int i = 0; i < count; i++) {
                    mChecked.put(i, checkbox.isChecked());
                }

                adapter.notifyDataSetChanged();

            }
        });


        isNotAdded = false;
    }

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if (position == 0) {
            } else {
                position = position - 1;
            }

        }
    });
}


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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.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.setName(jsonObject.getString("name"));
                    getter_setter.setEmail(jsonObject.getString("email"));

                    user_list.add(getter_setter);
                    adapter = new UserInfoAdapter(MainActivity.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);
    }
}

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) {

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.user_info_adapter, null);

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

            try {
                Name.setText(user_info_list.get(position).getName());
                Email.setText(user_info_list.get(position).getEmail());

                mcheckBox.setOnCheckedChangeListener(
                        new CompoundButton.OnCheckedChangeListener() {

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

                                    mChecked.put(position, isChecked);

                                    if (isAllValuesChecked()) {

                                        checkbox.setChecked(isChecked);
                                    }

                                } else {

                                    mChecked.delete(position);

                                    checkbox.setChecked(isChecked);

                                }

                            }
                        });

                mcheckBox.setChecked((mChecked.get(position) == true ? true : false));


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return convertView;
    }

    protected boolean isAllValuesChecked() {

        for (int i = 0; i < count; i++) {
            if (!mChecked.get(i)) {
                return false;
            }
        }

        return true;
    }
   }
 }

Getter_Setter:

public class Getter_Setter {

private String Name;
private String Email;

public boolean isclicked() {
return isclicked;
}

public void setIsclicked(boolean isclicked) {
this.isclicked = isclicked;
}

public boolean isclicked;

public String getName() {
return Name;
}

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

public String getEmail() {
return Email;
}

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




Aucun commentaire:

Enregistrer un commentaire