Android Listview选中了多个项目

时间:2014-06-25 10:22:03

标签: android sqlite listview android-arrayadapter

我正在尝试使用自定义适配器将arraylist填充到列表视图中。正在从SQLite数据库中检索数据,这一切都正常。我遇到的问题是我需要能够在列表中选择许多不同的条目并获取数据来创建PDF报告。

我已经设法让选择工作到一定程度,但问题是,当我在列表中选择一个项目时,第三个项目之后,然后每三个项目也被选中。我用来填充列表视图的代码是:

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);

我尝试使用onItemClickListener并使用以下内容更改所选项目的背景:

                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();
                }

            });

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;
}

}

我已经阅读并尝试了多种不同的东西,但我正在努力以我想要的方式工作。有没有人对我可能做错了什么有任何建议或提示。

干杯

1 个答案:

答案 0 :(得分:0)

您需要执行以下操作:

  1. boolean课程中添加isSelected标记Results,默认情况下将其设置为false。此标志将用于设置背景颜色。
  2. getView()标志的基础上设置ResultsAdapter isSelected的背景颜色。
  3. 切换isSelected的值并在onItemClick()中设置背景颜色。