1

I have a listview as text in left and checkboxes in the right side.My listview displays the inbox with sms body and address from which it comes.Now i want to get the address when i click the checkbox and to save it using button.my checkbox is clickable and my listview cannot be clicked since it appears as text. My code is below:

public class SMSInbox extends Activity{

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

        final ListView lViewSMS = (ListView) findViewById(R.id.list);  

        if(fetchInbox()!=null)  
        {  
            String[] from = new String[]{};

            int to = R.id.text1;
            ArrayAdapter adapter = new ArrayAdapter(this,R.layout.row,to,fetchInbox());  

            lViewSMS.setAdapter(adapter);  
        }


        }    


    public ArrayList fetchInbox()  
    {  
        ArrayList sms = new ArrayList();  

        Uri uriSms = Uri.parse("content://sms/inbox");  
        Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);   

        cursor.moveToFirst();  
        while  (cursor.moveToNext())  
        {  
               String address = cursor.getString(1);  
               String body = cursor.getString(3);  

               System.out.println("======> Mobile number => "+address);  
               System.out.println("=====> SMS Text => "+body);  

               sms.add("Address=> "+address+"\n SMS => "+body);  
        }  
        return sms;  

    }


        }

My XML code inbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/get_view_button" 
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="Get CheckBox" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

My row.xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
</TextView>

<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>

</RelativeLayout>

View code:

    public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

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

public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.row, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.text1);
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        viewHolder.checkbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        Model element = (Model) viewHolder.checkbox
                                .getTag();
                        element.setSelected(buttonView.isChecked());



                    }
                });
        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));

    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));

    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());
    holder.checkbox.setChecked(list.get(position).isSelected());
    return view;
}}

Model.java

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

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isSelected() {
    return selected;
}

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

let me know what to do to get the address from smsinbox.java in a checkbox click.And how to confirm that is checkbox get the address?

Please help me regarding this.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
divya
  • 81
  • 4
  • 13
  • man, you've never used your InteractiveArrayAdapter, you've created it but used an ordinary ArrayAdapter... am I missing some thing here??...which one of them (InteractiveArrayAdapter or ArrayAdapter) will you use? – a fair player Jan 21 '12 at 06:02
  • make me clear how to use InteraractiveArrayAdapter instead of ArrayAdapter in code format. – divya Jan 21 '12 at 06:08
  • instead of using "ArrayAdapter adapter = new ArrayAdapter(this,R.layout.row,to,fetchInbox());" you'll use "InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(this, model);" ..... by the way, both are correct but the issue is that you think that you are using some thing (InteractiveArrayAdapter) while actually you are not (because you are using ArrayAdapter). – a fair player Jan 21 '12 at 06:15
  • Welll , divyaaa aurr kyaa haal chaal hahaha :D as far as your answer is concerned i can give the full code and will help you – Raghav Chopra Jan 21 '12 at 06:33
  • basically u wanna save the address with the help of ur button..? are u sing a web service to save your address? , or u just donna wsave u just wanna get the addres on the click of checkbox? – Raghav Chopra Jan 21 '12 at 06:37
  • Yes First I want to get the address on the click of checkbox and i want to confirm it using toast.After succeed it,I will try to save the address.I didn't use any web service. – divya Jan 21 '12 at 06:44

2 Answers2

0

Add this method in your adapter :

First define new ArrayList for list of item selected:

ArrayList<String> listChosen = new ArrayList<>();

Create a method that return list :

/**
     * create this method for return new value in list item selected
     * @return
     */
    private List<String> getSelectedString(){
        return itemSelected;
    }

in your checkbox condition, do like the following code :

 holder.cbItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    itemSelected.add(itemName);

                    String jsonString = gson.toJson(getSelectedString());
                    edit.putString(Constants.KEY_SHARED,jsonString);
                    edit.commit();
                }else{
                    itemSelected.remove(itemName);
                    String jsonString = gson.toJson(getSelectedString());
                    edit.putString(Constants.KEY_SHARED,jsonString);
                    edit.commit();
                }
            }
        });

if you still confused how the process, i found a useful article, please refer to this article. Maybe there you'll find what you need. Hope it helps.

R Besar
  • 574
  • 1
  • 5
  • 12
0

I was having same issue, check out my question as well as answer.

Custom listView multiselection

All The Best

Community
  • 1
  • 1
Richa
  • 3,165
  • 1
  • 22
  • 26
  • Richa,I can't get since i am new to android.explain me what i have to change to get the listview values on checkbox click.I saw your code.There u have OnItemClickListener so u can click your listview.Then it can response and we can confirm it via toast.but i display listview appear in textview.I can't click the the listview value since it appear only as text.But I can click the checkbox.How I realize the response ofcheckbox using toast.any idea? – divya Jan 21 '12 at 07:26
  • make sure that ur views under row is focusable= "false" and clickable="false" – Richa Jan 21 '12 at 09:14
  • I put false in xml as you said.Eventhough problem in checkbox,couldn't select checkbox.Atleast i want to check through toast whether checkbox is clicked – divya Jan 21 '12 at 09:30