vendredi 21 octobre 2016

when a particular check box in checked ,To check all the checkboxes in alert dialoug having custom listview

public class DataBaseHandler extends SQLiteOpenHelper {
    SQLiteDatabase db = null;
    Cursor cursor;

    static int DBVersion = 1;


    public DataBaseHandler(MainActivity mainActivity, String doc_speciality, Object o, int i) {
        super(mainActivity, doc_speciality, (SQLiteDatabase.CursorFactory) o, i);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_CONTACTS_TABLE = "CREATE TABLE days(id INTEGER , dayname TEXT , checked TEXT )";
        db.execSQL(CREATE_CONTACTS_TABLE);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
        db.execSQL("DROP TABLE IF EXISTS days");
        // Create tables again
        onCreate(db);

        // db.close();
    }

    public void insert(ContentValues con) {

        SQLiteDatabase data = getWritableDatabase();
        data.insert("days", null, con);

    }

    public List<String> getAllLabels() {
        List<String> list = new ArrayList<String>();
        // Select All Query
        String selectQuery = "SELECT  * FROM days";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);//selectQuery,selectedArguments
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                list.add(cursor.getString(1));//adding 2nd column data
            } while (cursor.moveToNext());
        }
        // closing connection
        cursor.close();
        db.close();
        // returning lables
        return list;
    }

    public ArrayList<HashMap<String, String>> getList() {

        ArrayList<HashMap<String, String>> ImageTypeList;
        HashMap<String, String> imageType;
        String selectQuery = "SELECT  * FROM  days ";

        db = this.getReadableDatabase();
        cursor = db.rawQuery(selectQuery, null);
        ImageTypeList = new ArrayList<HashMap<String, String>>();
        if (cursor.moveToFirst()) {
            do {
                imageType = new HashMap<String, String>();
                imageType.put("id", cursor.getString(0));
                imageType.put("dayname", cursor.getString(1));
                imageType.put("checked", cursor.getString(2));
                ImageTypeList.add(imageType);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return ImageTypeList;
    }


    public void update(String sel) {
        String _id = sel;
        db = this.getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put("checked", "true");
        db.execSQL("UPDATE days SET checked='true' WHERE id = '" + _id + "' ");
    }

    public void updateuncheck(String unsel_id) {
        String _id = unsel_id;
        db = this.getWritableDatabase();
        ContentValues args = new ContentValues();
        args.put("checked", "false");
        db.execSQL("UPDATE days SET checked='false' WHERE id = '" + _id + "' ");

    }
}
public class CusAdapter extends BaseAdapter {
    Context mContext;

    ArrayList<HashMap<String, String>> mLst, mSelected_item, mUnselected_item;


    LayoutInflater mInflter;

    public CusAdapter(Context context, ArrayList<HashMap<String, String>> hash) {
        this.mContext = context;
        mLst = hash;
        mSelected_item = new ArrayList<HashMap<String, String>>();
        mUnselected_item = new ArrayList<HashMap<String, String>>();


        mInflter = (LayoutInflater.from(context));
    }

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

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

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

    public ArrayList<HashMap<String, String>> getUnselectedListItem() {
        return mUnselected_item;
    }


    public ArrayList<HashMap<String, String>> getListItem() {
        return mSelected_item;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflter.inflate(R.layout.splist, null);
        final CheckedTextView mChecked_text = (CheckedTextView) convertView.findViewById(R.id.my_checkedtextview);
        final HashMap<String, String> day_detail_hash = mLst.get(position);

        mChecked_text.setText(day_detail_hash.get("dayname"));
        mChecked_text.setTag(day_detail_hash.get("id"));
        String string_checked = day_detail_hash.get("checked");

        if (string_checked.equalsIgnoreCase("false")) {

            mChecked_text.setChecked(false);


        } else {
            mChecked_text.setChecked(true);
            mSelected_item.remove(day_detail_hash);
            mSelected_item.add(day_detail_hash);

        }

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

                if (mChecked_text.isChecked()) {
                    mChecked_text.setChecked(false);
                    mUnselected_item.add(day_detail_hash);
                    mSelected_item.remove(day_detail_hash);
                } else {
                    mChecked_text.setChecked(true);
                    mSelected_item.remove(day_detail_hash);
                    mSelected_item.add(day_detail_hash);
                    mUnselected_item.remove(day_detail_hash);

                }


            }
        });

        return convertView;
    }
}

I have a custom list view adapter with a checked text view.What here i want is to check all these listview checkboxes at once when i check one checkbox named all(is not mentioned here).

I don't know where to add the checkbox named all and also how to do.

public class MainActivity extends AppCompatActivity {

    DataBaseHandler mDataBase;
    ContentValues mContentValue;
    Context context;
    Button but;
    EditText mdays;
    String sel_id, unsel_id;
    CusAdapter custom;
    public static ArrayList<HashMap<String, String>> mArraylistHash = new ArrayList<HashMap<String, String>>();

    String[] id = {
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7"
    };
    String[] days = {
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
    };

    String[] Checked = {
            "false",
            "false",
            "false",
            "false",
            "false",
            "false",
            "false"
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = MainActivity.this;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mdays = (EditText) findViewById(R.id.edit_text);
        but = (Button) findViewById(R.id.but);
        mContentValue = new ContentValues();
        mDataBase = new DataBaseHandler(MainActivity.this, "days", null, 1);

        List<String> a = mDataBase.getAllLabels();

        if (a.size() == 0) {

            for (int i = 0; i < days.length; i++) {
                String num = days[i];
                mContentValue.put("dayname", num);
                mContentValue.put("checked", Checked[i]);
                mContentValue.put("id", id[i]);
                mDataBase.insert(mContentValue);
            }
        }

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), Main2Activity.class);
                startActivity(i);
            }
        });

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

                show();
            }

        });


    }

    private void show() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Choose One or More Days");

        LayoutInflater inflater = getLayoutInflater();

        View PopupLayout = inflater.inflate(R.layout.listxml, null);
        builder.setView(PopupLayout);

        mArraylistHash = mDataBase.getList();

        final ListView mListview = (ListView) PopupLayout.findViewById(R.id.listview);
        custom = new CusAdapter(context, mArraylistHash);

        mListview.setAdapter(custom);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                ArrayList<HashMap<String, String>> checked = custom.getListItem();
                ArrayList<HashMap<String, String>> unchecked = custom.getUnselectedListItem();

                String selected = "";

                for (HashMap<String, String> i : checked) {

                    sel_id = i.get("id").toString();
                    mDataBase.update(sel_id);

                    String sel = i.get("dayname").toString();

                    selected += sel + ", ";


                }

                for (HashMap<String, String> i : unchecked) {

                    unsel_id = i.get("id").toString();
                    mDataBase.updateuncheck(unsel_id);
                }
                mdays.setText(selected);


            }
        });

        builder.setNegativeButton(
                "cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.show();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



Aucun commentaire:

Enregistrer un commentaire