2016-02-19 58 views
0

我使用这个代码listview的背景颜色没有改变。我如何解决它?

ListView lv = getListView(); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      TextView idno = (TextView) view.findViewById(R.id.idno); 
      TextView fullname = (TextView) view.findViewById(R.id.fullname); 
      TextView remark = (TextView) view.findViewById(R.id.remark); 
      new DisplayStudent().execute("Update", idno.getText().toString()); 
      if(remark.getText().toString().equals("absent")) { 
       if(isStudentLate) { 
        view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorLate)); 
        fullname.setTextColor(Color.WHITE); 
        remark.setText("late"); 
       } 
       else { 
        view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPresent)); 
        fullname.setTextColor(Color.WHITE); 
        remark.setText("present"); 
       } 
      } 
      else if(remark.getText().toString().equals("present") || remark.getText().toString().equals("late")) { 
       view.setBackgroundColor(Color.WHITE); 
       fullname.setTextColor(Color.BLACK); 
       remark.setText("absent"); 
      } 
     } 
    }); 
    ((BaseAdapter)getListView().getAdapter()).notifyDataSetChanged(); 

每次我点击一个项目,该项目的背景颜色我拍了拍应该改变。为什么它没有改变?我试图删除((BaseAdapter)getListView().getAdapter()).notifyDataSetChanged();它确实会改变,但当我向下滚动并再次向上滚动时,它会回到原来的背景颜色。

例如。一个项目的背景颜色是白色的。当我点击该项目时,它变为绿色。那很完美。但是,当我向下滚动并再次向上滚动时,我点击的项目变回白色。我该如何解决?

+0

这是因为列表视图重用自己的看法。您必须为学生维护一个单独的数组或哈希映射,以确定它们是否存在或不存在。然后在你的listAdapter的getView中使用这个信息来相应地膨胀视图。 – Frosty

+0

像frosty说Listview回收它的行。 。根据您的选择,重构您的代码以更改Adapter.getView方法中的背景颜色。 –

回答

0

尝试在你的ListView标签设置这样的:

android:cacheColorHint="#00000000" 

而且也试试这个:

android:background="color code here"