mardi 1 décembre 2015

how to get text of checked checkbox in listview and transfer to another activity in android?

I want to get text of checked checkbox in listview and transfer them to second activity on button click ..i need some code to do this....this is what i done so far..

public class MainActivity extends Activity {

ListView listView;
ArrayAdapter<Model> adapter;
Button btest;
List<Model> list = new ArrayList<Model>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btest = (Button) findViewById(R.id.btest);
    listView = (ListView) findViewById(R.id.list);
  //  adapter = new CustomAdapter(this,getModel());
    adapter = new CustomAdapter(this,android.R.layout.simple_list_item_multiple_choice,getModel());
  //  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, getModel());
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);
    final ArrayList<String> selectedStrings = new ArrayList<String>();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView tvcheck = (TextView) view.getTag(R.id.name);
            CheckBox cb = (CheckBox) view.getTag(R.id.cbCategory);
           if (cb.isChecked()) {
               cb.setChecked(false);
           }else {
               cb.setChecked(true);
           }
             /*
                selectedStrings.add(tvcheck.getText().toString());
                Intent i = new Intent(getApplicationContext(), ResultActivity.class);
               // Bundle b = new Bundle();
              //  b.putString("selectedStrings", selectedStrings);
                i.putExtra("SelectedStrings", selectedStrings);
               // i.putExtras(b);
                startActivity(i);*/

            Toast.makeText(view.getContext(), tvcheck.getText().toString()+ "" +isCheckedOrNot(cb), Toast.LENGTH_LONG).show();
        }
    });
    btest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SparseBooleanArray checked = listView.getCheckedItemPositions();

            ArrayList<Model> selectedItems = new ArrayList<>();
            for (int i =0; i< checked.size(); i++){
                int pos = checked.keyAt(i);
                if (checked.valueAt(i))
                    selectedItems.add(adapter.getItem(pos));
            }
            String[] output = new String[selectedItems.size()];
            for (int i = 0; i< selectedItems.size(); i++){
                output[i] = String.valueOf(selectedItems.get(i));
            }
            Intent i = new Intent(getApplicationContext(), ResultActivity.class);
            Bundle b = new Bundle();
            b.putStringArray("selectedItems", output);
            i.putExtras(b);
            startActivity(i);
        }
    });
}

private String isCheckedOrNot(CheckBox checkBox){
    if(checkBox.isChecked())
        return "is checked";
    else
        return "is not checked";
}
private List<Model> getModel() {

    list.add(new Model("pulses"));
    list.add(new Model("tea"));
    list.add(new Model("Milk"));
    list.add(new Model("Sugar"));
    list.add(new Model("Salt"));
    list.add(new Model("Rice"));
    list.add(new Model("Bread"));
    list.add(new Model("Charger"));
    list.add(new Model("Mouse"));
    list.add(new Model("Mobile"));
    list.add(new Model("Laptop"));
    list.add(new Model("Bulb"));

    return list;
}

}

public class CustomAdapter extends ArrayAdapter<Model> {

private final List<Model> list;
private final Activity context;

public CustomAdapter(Activity context ,int res,List<Model> list){
    super(context, R.layout.view_item,res,list);
    this.context = context;
    this.list = list;
}
static class ViewHolder{
    protected TextView tvcheck;
    protected CheckBox cb;
}

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

    ViewHolder viewHolder = null;
    if(convertView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.view_item, null);
         viewHolder = new ViewHolder();
        viewHolder.tvcheck = (TextView) convertView.findViewById(R.id.name);
        viewHolder.cb = (CheckBox) convertView.findViewById(R.id.cbCategory);
      //  final ViewHolder finalViewHolder = viewHolder;
        viewHolder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                 int getPosition = (Integer)buttonView.getTag();
                list.get(getPosition).setSelected(buttonView.isChecked());
            }
        });
        convertView.setTag(viewHolder);
        viewHolder.cb.setTag(list.get(position));
        convertView.setTag(R.id.name, viewHolder.tvcheck);
        convertView.setTag(R.id.cbCategory, viewHolder.cb);
    }else {
        viewHolder = (ViewHolder)convertView.getTag();

    }

    viewHolder.cb.setTag(position);
    viewHolder.tvcheck.setText(list.get(position).getName());
    viewHolder.cb.setChecked(list.get(position).isSelected());

    return convertView;
}

}

public class ResultActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);

    Bundle b = getIntent().getExtras();
    String[] resultArr = b.getStringArray("selectedItems");
    ListView lv = (ListView) findViewById(R.id.outputlist);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,resultArr );
    lv.setAdapter(adapter);
}

}

public class Model {
private String name;
private boolean selected;

public Model(String name){
    this.name = name;
    selected = false;
}

public String getName(){
    return name;
}

public boolean isSelected(){
    return selected;
}
public void setSelected(boolean selected){
    this.selected = selected;
}

}




Aucun commentaire:

Enregistrer un commentaire