0

enter image description hereenter image description here I am new to android,I have one edittext with imageicon at right side,after click on that icon next activity will open, there I am using listview with check box,so problem is I want to get all selected items of listview on previous page's edittext,can any body help? Thanks

MyCustomAdapter dataAdapter = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_religionlist);

    // Generate list View from ArrayList
    displayListView();

    checkButtonClick();

}

private void displayListView() {

    // Array list of countries
    ArrayList<MotherTongues> stateList = new ArrayList<MotherTongues>();

    MotherTongues _states = new MotherTongues("Sikh", false);
    stateList.add(_states);
    _states = new MotherTongues("Parsi", true);
    stateList.add(_states);
    _states = new MotherTongues("Brahmin", false);
    stateList.add(_states);
    _states = new MotherTongues("Patel", true);
    stateList.add(_states);
    _states = new MotherTongues("Baniya", true);
    stateList.add(_states);
    _states = new MotherTongues("Rajput", false);
    stateList.add(_states);
    _states = new MotherTongues("Christian", false);
    stateList.add(_states);
    _states = new MotherTongues("Darji", false);
    stateList.add(_states);
    _states = new MotherTongues("Muslim", false);
    stateList.add(_states);

    // create an ArrayAdaptar from the String Array
    dataAdapter = new MyCustomAdapter(this, R.layout.list_item_religionlist, stateList);
    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            MotherTongues state = (MotherTongues) parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(),
                    "Clicked on : " + state.getName(), Toast.LENGTH_LONG)
                    .show();
        }
    });
}

private class MyCustomAdapter extends ArrayAdapter<MotherTongues> {

    private ArrayList<MotherTongues> stateList;

    public MyCustomAdapter(Context context, int textViewResourceId,

    ArrayList<MotherTongues> stateList) {
        super(context, textViewResourceId, stateList);
        this.stateList = new ArrayList<MotherTongues>();
        this.stateList.addAll(stateList);
    }

    private class ViewHolder {
        TextView code;
        CheckBox name;
    }

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

        ViewHolder holder = null;

        Log.v("ConvertView", String.valueOf(position));

        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.list_item_mothertongue, null);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.code);
            holder.name = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);

            convertView.setTag(holder);

            holder.name.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    MotherTongues _state = (MotherTongues) cb.getTag();

                    Toast.makeText(
                            getApplicationContext(),
                            "Checkbox: " + cb.getText() + " -> "
                                    + cb.isChecked(), Toast.LENGTH_LONG)
                            .show();

                    _state.setSelected(cb.isChecked());
                }
            });

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MotherTongues state = stateList.get(position);

        //holder.code.setText(" (" + state.getCode() + ")");
        holder.name.setText(state.getName());
        holder.name.setChecked(state.isSelected());

        holder.name.setTag(state);

        return convertView;
    }

}

private void checkButtonClick() {

    Button myButton = (Button) findViewById(R.id.findSelected);

    myButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            StringBuffer responseText = new StringBuffer();
            responseText.append("Selected Languages are...\n");

            ArrayList<MotherTongues> stateList = dataAdapter.stateList;

            for (int i = 0; i < stateList.size(); i++) {
                MotherTongues state = stateList.get(i);

                if (state.isSelected()) {
                    responseText.append("\n" + state.getName());
                    Intent intent=new Intent(ReligionList.this,PhotosFragment.class);
                    startActivity(intent);

                }
            }

            Toast.makeText(getApplicationContext(), responseText,
                    Toast.LENGTH_SHORT).show();
        }
    });
}

}

Deepzz
  • 4,573
  • 1
  • 28
  • 52

2 Answers2

0

You would need to save that data in array or some other data structure.

Example:

List<String> selectionList =new ArrayList<String>();
listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // When clicked, show a toast with the TextView text
        MotherTongues state = (MotherTongues) parent.getItemAtPosition(position);
        Toast.makeText(getApplicationContext(),
                "Clicked on : " + state.getName(), Toast.LENGTH_LONG)
                .show();

        //check if its checkbox is checked, and if so add from list else remove from list
        if(isChecked)
            selectionList.add(state.getName());
        else
            selectionList.remove(state.getName());
    }
});

Finally in your button click or wherever you want to send data back:

String [] selectionArray = selectionList.toArray(new String[selectionList.size()]);

And you can pass the String array to another activity using intent.putExtra()

intent.putExtra("selectionArray", selectionArray );

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0
  1. In Activity1 when you click on edit text icon, you can start your activity using startActivityforResult so that you can get back the result onActivityResult method like this..

    Intent intent = new Intent(Activity1.this, Activity2.class); startActivityForResult(intent, 0);

In Same Activity1 add

         @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);




             if (requestCode==0&&resultCode == RESULT_OK) {

            //recieve the arraylist of mother tounges sent from Activity2 with values set to true       for checked and false for unchecked...like this 

          ` data.getExtras().getStringArray(key);`


           //refresh your list view using arraylist recieved here from Activit2..



            }
     }
  1. In your Activity2 update arraylist with true/false values when mother tounges are check/unchecked...

and on ok button click, finish your activity this way..

 Intent intent = new Intent();
            intent.putExtra(key, your_arraylist);
            setResult(RESULT_OK, intent);
            finish();

this will send back the data in your Activity1's onActivityResult() method

iMDroid
  • 2,108
  • 1
  • 16
  • 29