如果复选框在ListView中如何实现OnCheckedChangeListener

时间:2012-02-20 22:33:28

标签: android listview checkbox

我成功在ListView中的复选框中显示电话联系人但是如果我单击按钮则无法检查选中了哪个复选框 这是我使用的代码----->

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);     
}
     }

我使用的布局是:

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 ----&gt;

   <?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>

我想如果我检查其中一个复选框就像Toast那样......

3 个答案:

答案 0 :(得分:1)

假设您正在使用viewHolder,这通常是最好的方法 http://www.google.com/events/io/2010/sessions/world-of-listview-android.html 或pdf http://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

所以你正在使用一个包含复选框的viewHolder。覆盖getView方法时,您将执行以下操作:

@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;
}

}

我没有编写其余的必需代码,因为你只是要求实现oncheckchangelistener。

希望有所帮助

答案 1 :(得分:0)

android:onClick="onCheckedChanged"添加到xml中的复选框。

答案 2 :(得分:0)

看看这个Link.it可能对你有所帮助。 http://www.vogella.de/articles/AndroidListView/article.html