我有一个列表并使用适配器我正在添加项目。当我自动选择第一项复选框后,选择滚动后的第一项,以及后续滚动的项目。如何解决的问题是什么。
<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
package 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);
}
}
答案 0 :(得分:0)
总是有一个模型并在适配器中处理数据是一个很好的做法
适配器中的模型
class Model {
String name;
String selected;
}
您的适配器getView
public View getView(int position, View convertView, ViewGroup parent) {
Model model = getItem(position);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = this.mActivity.getLayoutInflater();
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.chbx);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(model.getName());
holder.checkbox.setChecked(model.isSelected);
holder.checkbox.setOnCheckedChangeListener(this);
holder.checkbox.setTag(position)
return convertView;
}
Change you selected value in onChangelistener of CheckBox and make a notifyDataSetChange.
@Override
public void onCheckedChanged(CompoundButton checkbox,
boolean isChecked) {
// TODO Auto-generated method stub
int position = (Integer)checkbox.getTag();
Model model = getItem(position);
if (isChecked)
{
model.isSelected = true;
}
else
{
model.isSelected = false;
}
mModels.set(position,model);
notifyDataSetChanged();
}
答案 1 :(得分:0)
试试这个,
class MyAddressAdapter extends ArrayAdapter<String>
{
List<String> address;
List<Integer> checkedlist;
public MyAddressAdapter(Context context, int resource,
List<String> objects) {
super(context, resource, objects);
address=objects;
System.out.println("---->"+address.size());
for(int i=0;i<address.size();i++)
checkedlist.add(0); //initializing list
// 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.setId(position);
if(checkedlist.get(ctv.getId())==1)
ctv.setChecked(true);
else
ctv.setChecked(false);
ctv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("Position "+ isChecked);
checkedlist.set(buttonView.getId(),1);
}
else
checkedlist.set(buttonView.getId(),0);
notifydatasetchanged();
}
});
return row;
}
}
}