多选ListView无法正常工作

时间:2015-04-19 14:20:19

标签: android listview android-listview

所以我设法让我的列表视图突出显示所选项目,但我现在遇到了一个新问题,如果我选择一个项目,它突出显示,然后一旦我向下滚动,另一个项目突出显示,我知道问题是因为我的listview适配器回收视图以优化性能,我只是想知道,我如何阻止它做这个? 这是我的片段

public class SecondFragment extends Fragment {
CardDatabaseAdapter2 mCardDatabaseAdapter;
ExerciseHistoryAdapter mExerciseHistoryAdapter;
ArrayList<Set> sets = new ArrayList<>();
ArrayList<Set> selected = new ArrayList<>();
String name;
ListView lv;
int x;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.exercise_history, container, false);
    Bundle extras = getActivity().getIntent().getExtras();
    if (extras != null) {
        name = extras.getString("ExerciseName");
    }
    mCardDatabaseAdapter = new CardDatabaseAdapter2(getActivity());
    sets = mCardDatabaseAdapter.getAllSetsWithName(name);
    Log.v("array", sets.toString());

    lv = (ListView) rootView.findViewById(R.id.history_list);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(!selected.contains(sets.get(position))) {
                lv.setItemChecked(position, true);
                view.setBackgroundColor(Color.rgb(255, 152, 0));
                Toast.makeText(getActivity(), "Item Selected", Toast.LENGTH_SHORT).show();
                selected.add(sets.get(position));
            } else {
                lv.setItemChecked(position, false);
                view.setBackgroundColor(Color.rgb(221, 221, 221));
                selected.remove(sets.get(position));
                Toast.makeText(getActivity(), "Item Unselected", Toast.LENGTH_SHORT).show();
            }
        }
    });



    Button add = (Button) rootView.findViewById(R.id.add_button);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });

    mExerciseHistoryAdapter= new ExerciseHistoryAdapter(getActivity(), R.layout.history_listview_item, sets);
    lv.setAdapter(mExerciseHistoryAdapter);

    return rootView;
}
}

这是我的适配器

public class ExerciseHistoryAdapter extends ArrayAdapter {


Context mContext;
int mLayoutResourceId;
ArrayList<Set> mData;
PlaceHolder holder;


public ExerciseHistoryAdapter(Context context, int resource, ArrayList<Set> data) {
    super(context, resource, data);
    this.mContext = context;
    this.mLayoutResourceId = resource;
    this.mData = data;
}

@Override
public Object getItem(int position) {
    return super.getItem(position);
}

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

    View row = convertView;
    holder = null;
    //inflate the layout for a single row

    if(row == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(mLayoutResourceId, parent, false);

        holder = new PlaceHolder();

        holder.weight = (TextView) row.findViewById(R.id.rowWeight);
        holder.reps = (TextView) row.findViewById(R.id.rowReps);

        row.setTag(holder);

    } else {
        //otherwise use an existing one
        holder = (PlaceHolder) row.getTag();
        Log.v("Position", "i " + position);

    }
    //getting the data from the data array
    Set place = mData.get(position);

    //setting the view to reflect the data we need to display

    holder.weight.setText(String.valueOf(place.getWeight()));
    holder.reps.setText(String.valueOf(place.getReps()));
    holder.reps.setTextColor(Color.GRAY);
    holder.weight.setTextColor(Color.GRAY);

    //returning the row
    return row;
}




private static class PlaceHolder {

    TextView weight;
    TextView reps;

}
}

1 个答案:

答案 0 :(得分:1)

getView()方法中,您需要检查选择状态并相应地更新UI。

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

    View row = convertView;
    holder = null;
    //inflate the layout for a single row

    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(mLayoutResourceId, parent, false);

        holder = new PlaceHolder();

        holder.weight = (TextView) row.findViewById(R.id.rowWeight);
        holder.reps = (TextView) row.findViewById(R.id.rowReps);

        row.setTag(holder);

    } else {
        //otherwise use an existing one
        holder = (PlaceHolder) row.getTag();
        Log.v("Position", "i " + position);

    }
    //getting the data from the data array
    Set place = mData.get(position);

    //setting the view to reflect the data we need to display

    holder.weight.setText(String.valueOf(place.getWeight()));
    holder.reps.setText(String.valueOf(place.getReps()));
    holder.reps.setTextColor(Color.GRAY);
    holder.weight.setTextColor(Color.GRAY);

    // Update color based on selected state
    row.setBackgroundColor(
            lv.isItemChecked(position)
                    ? Color.rgb(255, 152, 0) // selected color
                    : Color.rgb(221, 221, 221) // normal color
    );

    //returning the row
    return row;
}

获取lv参考:

ListView lv;

public ExerciseHistoryAdapter(Context context, int resource, ArrayList<Set> data, ListView lv) {
    super(context, resource, data);
    this.mContext = context;
    this.mLayoutResourceId = resource;
    this.mData = data;
    this.lv = lv;
}

在你的片段中:

mExerciseHistoryAdapter= new ExerciseHistoryAdapter(getActivity(), R.layout.history_listview_item, sets, lv);