gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:28:29 25 4
gpt4 key购买 nike

我成功地在 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---->

   <?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 的事情..

最佳答案

假设您使用的是 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。

希望对你有帮助

关于android - 如果复选框在 ListView 中,如何实现 OnCheckedChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9369433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com