Android Listview选中多个项目
我正在尝试使用自定义适配器将arraylist填充到列表视图中。正在从SQLite数据库检索数据,一切正常。我遇到的问题是,我需要能够在列表中选择许多不同的条目,并获取数据以创建PDF报告 我已经设法使选择工作到一定程度,但问题是,当我在列表中选择一个项目时,后面的第三个项目和每三个项目也会被选择。我用来填充列表视图的代码是:Android Listview选中多个项目,android,sqlite,listview,android-arrayadapter,Android,Sqlite,Listview,Android Arrayadapter,我正在尝试使用自定义适配器将arraylist填充到列表视图中。正在从SQLite数据库检索数据,一切正常。我遇到的问题是,我需要能够在列表中选择许多不同的条目,并获取数据以创建PDF报告 我已经设法使选择工作到一定程度,但问题是,当我在列表中选择一个项目时,后面的第三个项目和每三个项目也会被选择。我用来填充列表视图的代码是: resultsView.setChoiceMode(resultsView.CHOICE_MODE_MULTIPLE); final ArrayList<Resul
resultsView.setChoiceMode(resultsView.CHOICE_MODE_MULTIPLE);
final ArrayList<Results> resultsList = (ArrayList<Results>) srsDB.getReulstByMeter(CurrentUser.currentMeter.getID());
final ResultsAdapter adapter = new ResultsAdapter(getActivity(),resultsList);
resultsView.setAdapter(adapter);
resultsView.setChoiceMode(resultsView.CHOICE\u MODE\u MULTIPLE);
最终ArrayList结果列表=(ArrayList)srsDB.getReulstByMeter(CurrentUser.currentMeter.getID());
最终结果适配器=新结果适配器(getActivity(),resultsList);
resultsView.setAdapter(适配器);
我已尝试使用和McClickListener更改所选项目的背景,方法如下:
resultsView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
resultsView.setSelection(position);
Log.i("Selectio", "" + position);
view.setSelected(!view.isSelected());
if(view.isSelected()){
view.setBackgroundColor(Color.GRAY);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
adapter.notifyDataSetChanged();
}
});
resultsView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//TODO自动生成的方法存根
resultsView.setSelection(位置);
Log.i(“选择”,位置“+”);
view.setSelected(!view.isSelected());
如果(view.isSelected()){
视图.背景颜色(颜色.灰色);
}否则{
视图.setBackgroundColor(颜色.透明);
}
adapter.notifyDataSetChanged();
}
});
ResultsAdapter代码如下所示:
public class ResultsAdapter extends ArrayAdapter<Results>{
private SRSDatabase srsDB;
private Meters meter;
private Customers customer;
private int mItemIndex = -1;
private int pos;
public ResultsAdapter(Context context, ArrayList<Results> results) {
super(context, R.layout.result_row, results);
srsDB = new SRSDatabase(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Results result = getItem(position);
pos = position;
if(convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.result_row,parent, false);
convertView.setSelected(false);
}
//Look up view for data population
TextView custName = (TextView) convertView.findViewById(R.id.customerNameRes);
TextView testDate = (TextView) convertView.findViewById(R.id.resultDate);
TextView meterSN = (TextView) convertView.findViewById(R.id.meterResSN);
TextView meterConst = (TextView) convertView.findViewById(R.id.meterResConst);
TextView meterRating = (TextView) convertView.findViewById(R.id.meterResRat);
TextView vA = (TextView) convertView.findViewById(R.id.resVa);
TextView vB = (TextView) convertView.findViewById(R.id.resVb);
TextView vC = (TextView) convertView.findViewById(R.id.resVc);
TextView iA = (TextView) convertView.findViewById(R.id.resIa);
TextView iB = (TextView) convertView.findViewById(R.id.resIb);
TextView iC = (TextView) convertView.findViewById(R.id.resIc);
TextView tA = (TextView) convertView.findViewById(R.id.resTa);
TextView tB = (TextView) convertView.findViewById(R.id.resTb);
TextView tC = (TextView) convertView.findViewById(R.id.resTc);
TextView frequency = (TextView) convertView.findViewById(R.id.resFreq);
TextView error = (TextView) convertView.findViewById(R.id.resError);
//Populate the data into the template using the data object
custName.setText(srsDB.getCustomerByID(result.getCustomerID()).getCustomer());
testDate.setText("" + result.getDate());
meter = srsDB.getMeterByID(result.getMeterID());
meterSN.setText("" + result.getID());
meterConst.setText("" + meter.getConstant());
meterRating.setText("" + meter.getRating());
vA.setText("" + result.getVA());
vB.setText("" + result.getVB());
vC.setText("" + result.getVC());
iA.setText("" + result.getIA());
iB.setText("" + result.getIB());
iC.setText("" + result.getIC());
tA.setText("" + result.getThetaA());
tB.setText("" + result.getThetaB());
tC.setText("" + result.getThetaC());
frequency.setText("" + result.getFrequency());
error.setText("" + result.getError());
return convertView;
}
公共类ResultsAdapter扩展了ArrayAdapter{
私人SRSDatabase srsDB;
私人电表;
私人客户;
私有int-mindex=-1;
私人国际邮政局;
公共结果适配器(上下文、ArrayList结果){
super(上下文、R.layout.result_行、结果);
srsDB=新的SRSDatabase(上下文);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终结果=getItem(位置);
pos=位置;
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.result\u行,父级,false);
convertView.setSelected(false);
}
//查找数据填充的视图
TextView custName=(TextView)convertView.findViewById(R.id.customerNames);
TextView testDate=(TextView)convertView.findViewById(R.id.resultDate);
TextView meterSN=(TextView)convertView.findViewById(R.id.MeterResn);
TextView meterConst=(TextView)convertView.findViewById(R.id.meterResConst);
TextView meterRating=(TextView)convertView.findViewById(R.id.meterResRat);
TextView vA=(TextView)convertView.findViewById(R.id.resVa);
TextView vB=(TextView)convertView.findViewById(R.id.resVb);
TextView vC=(TextView)convertView.findViewById(R.id.resVc);
TextView iA=(TextView)convertView.findViewById(R.id.resIa);
TextView iB=(TextView)convertView.findViewById(R.id.resIb);
TextView iC=(TextView)convertView.findViewById(R.id.resIc);
TextView tA=(TextView)convertView.findViewById(R.id.resTa);
TextView tB=(TextView)convertView.findViewById(R.id.resTb);
TextView tC=(TextView)convertView.findViewById(R.id.resTc);
TextView频率=(TextView)convertView.findViewById(R.id.resFreq);
TextView错误=(TextView)convertView.findViewById(R.id.resError);
//使用数据对象将数据填充到模板中
custName.setText(srsDB.getCustomerByd(result.getCustomerID()).getCustomer());
testDate.setText(“+result.getDate());
meter=srsDB.getMeterByID(result.getMeterID());
meterSN.setText(“+result.getID());
meterConst.setText(“+meter.getConstant());
meterRating.setText(“+meter.getRating());
vA.setText(“+result.getVA());
setText(“+result.getVB());
setText(“+result.getVC());
iA.setText(“+result.getIA());
iB.setText(“+result.getIB());
iC.setText(“+result.getIC());
tA.setText(“+result.getThetaA());
tB.setText(“+result.getThetaB());
tC.setText(“+result.getThetaC());
frequency.setText(“+result.getFrequency());
error.setText(“+result.getError());
返回视图;
}
}
我读过很多书,尝试过很多不同的东西,但我很难让任何东西按照我想要的方式工作。有没有人对我可能做错的事情有什么建议或建议
干杯您需要执行以下操作:
resultsView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
resultsView.setSelection(position);
Log.i("Selectio", "" + position);
view.setSelected(!view.isSelected());
if(view.isSelected()){
view.setBackgroundColor(Color.GRAY);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
adapter.notifyDataSetChanged();
}
});
Results
类中添加boolean
标志isSelected
,默认设置为false
。此标志将用于设置背景色isSelected
标志的基础上,在ResultsAdapter
的getView()
中设置背景色isSelected
的值,并在onItemClick()
中设置背景色李>
太好了,谢谢你的帮助。现在我只有我想要选择的项目。我不能,因为我只有3个代表。需要15个代表才能这样做,而且前几天才加入。你回答的时候我试过了。