【发布时间】:2015-04-30 15:15:18
【问题描述】:
我有一个列表并使用适配器向其中添加项目。当我自动选择复选框的第一项时,滚动后的第一项被选中,因此后续滚动的项目。有什么问题怎么解决。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.messagecleaner.MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select the addresses don't want to see" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idAddressList"
>
</ListView>
</LinearLayout>
</RelativeLayout>
enter code here
包 com.example.messagecleaner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.text.AndroidCharacter;
import android.content.Context;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
ListView lstAddress;
Context mContext;
List<String> arrAddress;
class MyAddressAdapter extends ArrayAdapter<String>
{
List<String> address;
public MyAddressAdapter(Context context, int resource,
List<String> objects) {
super(context, resource, objects);
address=objects;
System.out.println("---->"+address.size());
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
View row=convertView;
if(row==null)
{
LayoutInflater inflator= getLayoutInflater();
row = inflator.inflate(R.layout.addresscheckboxlistitem, parent, false);
CheckBox ctv=(CheckBox) row.findViewById(R.id.idAddressCheckTextView);
ctv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
System.out.println("Position "+ isChecked);
}
});
//ctv.setText(arrAddress[position]);
System.out.println(address.get(position));
ctv.setText(address.get(position));
return row;
}
else
{
CheckBox ctv=(CheckBox) row.findViewById(R.id.idAddressCheckTextView);
ctv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
System.out.println(address.get(position));
ctv.setText(address.get(position));
return row;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstAddress=(ListView) findViewById(R.id.idAddressList);
mContext=this;
arrAddress= new ArrayList<String>();
Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
//arrAddress=new String[cursor.getCount()];
int i=0;
if (cursor.moveToFirst()) { // must check the result to prevent exception
do {
String msgData = "";
for(int idx=0;idx<cursor.getColumnCount();idx++)
{
if(cursor.getColumnName(idx).equals("address"))
{
String msgAddress=cursor.getString(idx);
//msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
// System.out.println(msgAddress);
//arrAddress[i]=msgAddress;
if(arrAddress.contains(msgAddress))
{
}
else
{
arrAddress.add(msgAddress);
}
}
}
// use msgData
i++;
} while (cursor.moveToNext());
System.out.println("Address array "+arrAddress.size());
lstAddress.setAdapter(new MyAddressAdapter(mContext, android.R.layout.simple_list_item_1, arrAddress));
} else {
// empty box, no SMS
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
您必须将所选项目保存在您的模型类中并对您的适配器进行 notifyDatasetChange
标签: android listview checkbox selection