Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if the check box is within a ListView how to implement the OnCheckedChangeListener

I succeed to display Phone Contacts in a check Box within a ListView but if I click the button a can't check which check box is checked this a is the code I work with ----->

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
  import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
    import android.view.View;
 import android.view.View.OnClickListener;
  import android.view.Window;
   import android.view.WindowManager;
   import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
 import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
 import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Secure1 extends Activity implements OnCheckedChangeListener{



CheckBox ch1;
public static final String TAG = "ContactManager";

private Button bSave;
private ListView lv;
private boolean mShowInvisible;
CheckBox ch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.v(TAG, "Activity State: onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    bSave = (Button) findViewById(R.id.addContactButton);
    lv = (ListView) findViewById(R.id.contactList);
    ch = (CheckBox) findViewById(R.id.contactEntryText);        
    bSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Log.d(TAG, "mAddAccountButton clicked");
            //launchContactAdder();
        }
    });

    populateContactList();
}
private void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME
    };
    SimpleCursorAdapter adapter = new 
  SimpleCursorAdapter(this,R.layout.contact_entry  , cursor,   
  fields, new int[] {R.id.contactEntryText});
    lv.setAdapter(adapter);
 }

 private Cursor getContacts(){
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder =null

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     TODO Auto-generated method stub
    Toast text = Toast.makeText(getApplicationContext(), "wa3333333",
    Toast.LENGTH_SHORT);     
}
     }

and the layout i work with are :

main1.xml ---->

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
  <ListView android:layout_width="match_parent"
          android:id="@+id/contactList"
          android:layout_height="wrap_content"
          android:layout_weight="1"/>

  <Button android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/addContactButton"
        android:text="save"/>
   </LinearLayout> 

et aussi contact_entry.xml---->

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


  <CheckBox 
    android:text="@+id/contactEntryText"
          android:id="@+id/contactEntryText"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
    />
   </LinearLayout>

I want if I check one of those check boxes something happen like Toast ..

like image 853
Wael Ilahi Avatar asked Nov 17 '25 01:11

Wael Ilahi


1 Answers

Assuming you are using a viewHolder which is usually the best way of doing http://www.google.com/events/io/2010/sessions/world-of-listview-android.html or the pdf http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

So you are using a viewHolder which contains a checkbox. When overriding the getView method you'll do something like :

@Override
public View getView(int position, View aView, ViewGroup parent) {
    View view = null;
    if (aView == null) {
        ...
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.MyCheckbox);
        viewHolder.checkbox
             .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()                  
                 {
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView,
                          boolean isChecked) {
                      ...
                  }
              });
         ...
    } else {
        ...
    }
     ...
    return view;
}

}

I did not write the rest of the required code as you were only asking for the implementation of the oncheckchangelistener.

Hope it helps

like image 69
HpTerm Avatar answered Nov 19 '25 16:11

HpTerm