Android ListView:修改一行会影响其他行

时间:2014-10-14 09:01:32

标签: java android listview adapter

我目前正在创建一个包含带有ImageView的ListView的Activity作为复选框,如果用户点击该行的ImageView,它将发生变化。问题是当选择一行修改其ImageView时,但在浏览了几行(确切地说是8行)之后,我看到该行的ImageView也被修改了。

以下是一些相关代码:

HistoryRowAdapter.java

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater theInflater = LayoutInflater.from(getContext());
        convertView = theInflater.inflate(R.layout.history_row, parent, false);
        convertView.setTag(getItemId(position));
    }

    final Operation currentOperation = getItem(position);
    //Log.d("KS", "Name: " + currentOperation.toString());

    DBTools db = new DBTools(getContext());
    HashMap<String,String> patientData = db.getPatientData(currentOperation.getId());

    String patientName = "N/A";
    if (Integer.parseInt(patientData.get("id")) != -1){
        //Patient Data was entered

        String lastName = patientData.get("last_name");
        String firstName = patientData.get("first_name");
        patientName = lastName.length()!=0 && firstName.length()!=0 ? lastName+", "+firstName.charAt(0): "N/A";
    }

    TextView nameTextView = (TextView) convertView.findViewById(R.id.nameFieldTextView);
    nameTextView.setTextColor(Color.WHITE);
    nameTextView.setText(patientName);

    String[] dateTime = currentOperation.getDatetime().split("_");

    TextView dateTextView = (TextView) convertView.findViewById(R.id.dateFieldTextView);
    dateTextView.setTextColor(Color.WHITE);
    dateTextView.setText(dateTime[0]);

    dateTime[1] = dateTime[1].replace("-",":");
    TextView timeTextView = (TextView) convertView.findViewById(R.id.timeFieldTextView);
    timeTextView.setTextColor(Color.WHITE);
    timeTextView.setText(dateTime[1]);

    final ProgressDialog loading = new ProgressDialog(context);
    loading.setMessage("Loading Procedure...");
    loading.setCancelable(false);
    loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    Button viewOperationButton = (Button) convertView.findViewById(R.id.viewOperationButton);
    viewOperationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context, ReviewMode.class);
            i.putExtra("selectedOperation",currentOperation.getId());
            i.putExtra("deviceStatus",0);
            loading.show();
            context.startActivity(i);
        }
    });

    /*ImageView checkedImageView = (ImageView) theView.findViewById(R.id.checkedImageView);
    for (int i = 0; i< itemsSelected.size(); i++){
        if(position == itemsSelected.get(i)){
            checkedImageView.setImageResource(R.drawable.selected_row);
        }
    }*/


    return convertView;
}

“检查”行的功能:

public View getViewByPosition(int position, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (position < firstListItemPosition || position > lastListItemPosition ) {
        return listView.getAdapter().getView(position, null, listView);
    } else {
        final int childIndex = position - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

public void buttonSelect(){
    if (this.highlightedIndex != -1){
        int firstRow = proceduresListView.getFirstVisiblePosition();
        int lastRow = proceduresListView.getLastVisiblePosition();
        Log.d("KS","First Row: "+firstRow);
        Log.d("KS","Last Row: "+lastRow);
        //RelativeLayout highlightedRow = (RelativeLayout) proceduresListView.getChildAt(proceduresListView.getCheckedItemPosition()%7);
        Log.d("KS","Checked:"+proceduresListView.getCheckedItemPosition());
        RelativeLayout highlightedRow = (RelativeLayout) getViewByPosition(proceduresListView.getCheckedItemPosition(), proceduresListView);
        Log.d("KS","Selected:"+this.highlightedIndex);
        ImageView checkboxRow = (ImageView) highlightedRow.findViewById(R.id.checkedImageView);

        if (selectedItems.contains(this.highlightedIndex)){
            Log.d("KS","Items: "+selectedItems.toString());
            checkboxRow.setImageResource(R.drawable.normal_row);
            //selectedItems.remove(this.highlightedIndex);
            selectedItems.remove(selectedItems.indexOf(this.highlightedIndex));
        } else {
            checkboxRow.setImageResource(R.drawable.selected_row);
            selectedItems.add(this.highlightedIndex);
            Log.d("KS","Items: "+selectedItems.toString());
        }

        checkedCountTextView.setText(selectedItems.size()+"");
        //Log.d("KS", "Select: " + highlightedIndex);
        //Log.d("KS","Items: "+selectedItems.toString());
    }
}

0 个答案:

没有答案