2
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long   l) {

            if (selectedCourses.contains(courseNames.get(position))){
                selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position)));
                Toast.makeText(getApplicationContext(),
                      courseNames.get(position) + " removed", Toast.LENGTH_LONG)
                      .show();

                row = view;
                row.setBackgroundResource(R.drawable.charback);

                System.out.println(selectedCourses);
            }

            else{
                selectedCourses.add(courseNames.get(position));
                Toast.makeText(getApplicationContext(),
                        courseNames.get(position) +" added", Toast.LENGTH_LONG)
                          .show();

                row = view;
                row.setBackgroundResource(R.drawable.white);

                myarrayAdapter.notifyDataSetChanged();
                System.out.println(selectedCourses);

            }

选定的项目变白,但迟到了大约 12 个项目,另一个随机项目在进入视野时变白?有人可以帮帮我吗??

4

3 回答 3

0

将会发生的事情是您getView在适配器中的方法没有回收/重用convertView提供的方法。

如果convertView不为空,则表示它是一个正在被重用的视图。这意味着您应该将背景资源显式设置为默认值,除非该项目被选中。您看到重新使用白色背景的原因是因为您没有将背景重置为默认值。

就像是:

if (convertView != null) {
   if (selectedCourses.contains(courseNames.get(position))){
       convertView.setBackgroundResource(R.drawable.white);
   } else {
       convertView.setBackgroundResource(R.drawable.charback);
   }
}
于 2013-10-22T04:47:33.743 回答
0

尝试这个:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long   l) {

        if (selectedCourses.contains(courseNames.get(position))){
            selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position)));
            Toast.makeText(getApplicationContext(),
                  courseNames.get(position) + " removed", Toast.LENGTH_LONG)
                  .show();

            row = view;
            row.setBackgroundResource(R.drawable.charback);

            System.out.println(selectedCourses);
        }

        else{
            selectedCourses.add(courseNames.get(position));
            Toast.makeText(getApplicationContext(),
                    courseNames.get(position) +" added", Toast.LENGTH_LONG)
                      .show();

            row = view;
            row.setBackgroundResource(R.drawable.white);

            System.out.println(selectedCourses);

        }

我删除了 myarrayAdapter.notifyDataSetChanged(); 线....

于 2013-10-22T04:48:08.090 回答
0

您可以为此使用 Holder 类:

在您的适配器类中创建另一个包含您的组件的静态类:请参见下面的代码

static class MyHolder {

        TextView mTextView;
        RelativeLayout lv;
        ImageView imgInvite;
        RelativeLayout rel_bckground;
    }

之后在您的 getView 方法中使用它:

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

           View row = convertView;
            holder = null;

          if (row == null) {

         // initialize your components
         holder = new MyHolder();
         holder.mImageView = (ImageView) row.findViewById(R.id.someID);

        }else{

         holder = (MyHolder) row.getTag();
             }
    }
于 2013-10-22T05:17:09.487 回答