我添加了一个listadapter,其中包含一个复选框,imageview,两个textview和一个listview按钮。问题是,当我单击单个复选框以选择多个选中时。下面是我的适配器代码。请告诉我代码中有什么问题。
package com.mgm.schannel.lazylist;
import java.util.ArrayList;
import java.util.List;
import com.mgm.schannel.R;
import com.mgm.schannel.fonts.FontUtil;
import com.mgm.schannel.parser.inbox.Child;
import com.mgm.schannel.parser.inbox.Messages;
import com.mgm.schannel.ui.inboxActivity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class InboxAdapter extends BaseAdapter {
public ArrayList<Object> getSelectedItems() {
return selectedItems;
}
private ArrayList<Object> selectedItems;
private inboxActivity activity;
private List<Object> data;
private static LayoutInflater inflater = null;
private TextView MessageCount;
FontUtil fontsList;
private boolean showMoveButton;
int positionItem;
public TextView getMessageCount() {
return MessageCount;
}
public InboxAdapter(inboxActivity a, List<Object> results, FontUtil fontslist) {
activity = a;
data = results;
fontsList = fontslist;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
showMoveButton = false;
selectedItems = new ArrayList<Object>();
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Object inboxObject = data.get(position);
positionItem = position;
if(convertView == null)
vi = inflater.inflate(R.layout.inboxlist, null);
Button btnmove = (Button)vi.findViewById(R.id.btnmovehere);
if(position % 2 == 0)
{
vi.setBackgroundColor(Color.parseColor("#ebebeb"));
}
else
{
vi.setBackgroundColor(Color.parseColor("#f4f4f4"));
}
if(inboxObject instanceof Messages)
{
ImageView imgicon = (ImageView)vi.findViewById(R.id.inboxicon);
imgicon.setImageResource(R.drawable.message);
TextView text = (TextView)vi.findViewById(R.id.txtCaption);
text.setTextSize(16.f);
text.setTextColor(Color.BLACK);
text.setTypeface(fontsList.getTitilliumMaps29L002());
text.setText(((Messages)inboxObject).getSender());
text = (TextView)vi.findViewById(R.id.txtMessage);
text.setTextSize(10.f);
text.setTextColor(Color.BLACK);
text.setTypeface(fontsList.getTitilliumMaps29L002());
text.setText(((Messages)inboxObject).getMessage());
btnmove.setVisibility(View.INVISIBLE);
CheckBox cbSelected = (CheckBox)vi.findViewById(R.id.chkSelection);
cbSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(buttonView.isChecked())
{
getSelectedItems().add(data.get(positionItem));
}
}
});
}
else if(inboxObject instanceof Child)
{
TextView text = (TextView)vi.findViewById(R.id.txtCaption);
text.setTextSize(14.f);
text.setTextColor(Color.BLACK);
text.setTypeface(fontsList.getTitilliumMaps29L002());
Log.e("Folder Name", ((Child)inboxObject).getFolderName());
text.setText(((Child)inboxObject).getFolderName());
ImageView imgicon = (ImageView)vi.findViewById(R.id.inboxicon);
imgicon.setImageResource(R.drawable.folder);
text = (TextView)vi.findViewById(R.id.txtMessage);
text.setVisibility(View.GONE);
if(showMoveButton)
{
btnmove.setVisibility(View.VISIBLE);
}
else
{
btnmove.setVisibility(View.INVISIBLE);
}
CheckBox cbSelected = (CheckBox)vi.findViewById(R.id.chkSelection);
cbSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(buttonView.isChecked())
{
getSelectedItems().add(data.get(positionItem));
}
}
});
}
return vi;
}
public void showMove(boolean value) {
showMoveButton = value;
}
}
答案 0 :(得分:0)
您始终引用相同的复选框对象。您需要每次引用另一个,或者每次都创建一个新的复选框。
CheckBox cbSelected=(CheckBox)vi.findViewById(R.id.chkSelection);