mardi 24 mai 2016

Checkbox with listview

how can i get the value of checkedvalue of checkbox? here is the code i am trying to figure it out. i want to get the value of the checkbox in the listview

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

    bt1 = (Button) findViewById(R.id.button1);


    this.list = (ListView) findViewById(R.id.contact_list);
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    List<String> contact = databaseAccess.getQuotes();
    databaseAccess.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, contact);
    this.list.setAdapter(adapter);


    Contact = new Contact();
    service = new Service(this);
    init();
    getContent();
}


private void init() {
    list = (ListView) findViewById(R.id.contact_list);
    list.setCacheColorHint(Color.TRANSPARENT);
    //  list.setOnItemClickListener((AdapterView.OnItemClickListener) new ViewItemListener());
    //bt1.setOnClickListener(this);

    list.setOnItemClickListener(new ViewItemListener());
    email3 = (EditText) findViewById(R.id.email3);
    email4 = (EditText) findViewById(R.id.email4);


    Intent intent = getIntent();
    int id = intent.getIntExtra("id", -1);
    Contact = service.getById(id);
    email3.setText(Contact.getEmail());
    from = email3.getText().toString();
   // email3 = null;
}


private void getContent() {

    bt1 = (Button) findViewById(R.id.button1);
    list = (ListView) findViewById(R.id.contact_list);

    // List mylist = new ArrayList;
    List mylist = new ArrayList();
    String queryName = email4.getText().toString();
    contacts = service.getByName(queryName);


    if (contacts != null) {
        //    for (int i = 0; i < contacts.size(); i++) {
        for (int i = 0; i < contacts.size(); i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText(String.valueOf(contacts.get(i)));
            //cb.setId((Integer) contacts.get(i));


            Contact contact = (Contact) contacts.get(i);

            // HashMap
            HashMap map = new HashMap();
            if (contact.getDepartment().equals("IT")) {
                map.put("tv_image", R.drawable.icon_mis);


            }
            if (contact.getDepartment().equals("TD")) {
                map.put("tv_image", R.drawable.icon_td);

            }
            map.put("tv_name", contact.getName());
            map.put("tv_phone", contact.getEmail());
            map.put("tv_email", contact.getEmail());
            mylist.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.list_item,
                new String[]{"tv_image", "tv_name", "tv_email"},
                new int[]{R.id.user_image, R.id.item_name, R.id.item_phone});

        list.setAdapter(adapter);
        tv = (TextView) findViewById(R.id.textView);
        email3.setText(Contact.getEmail());
        // e3 = checkedValue.add(email3.getText().toString());
       //  e3 = getResources().getString(R.string.email);

    }
}



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

        cb = (CheckBox) findViewById(R.id.checkBox1);

        if (cb.isChecked()) {

            contact = (Contact) contacts.get(position);
            //checkedValue.get(Contact.getId());

           /// email3.setText(Contact.getEmail());
           checkedValue.add(email3.getText().toString());


            Toast.makeText(Main4.this, "" + email3.getText().toString() + checkedValue, Toast.LENGTH_LONG).show();
        }

else Toast.makeText(Main4.this, "No Email Add.", Toast.LENGTH_LONG).show();

        }
    }

    private EditText setText(String email) {
      email = contact.getEmail();
        return null;
    }
}




Aucun commentaire:

Enregistrer un commentaire