2016-11-10 87 views
0

我有通知列表视图,我想如果用户点击通知(特定行)它的颜色应该改变。 改变颜色意味着通知被读取。 即使重新启动应用程序,也应该反映已更改的颜色。如何更改列表视图的特定行颜色

为此,我已经在扩展阵列适配器的类中编写代码。

@Override 
public View getView(int position, View v, ViewGroup parent) 
{ 
    View mView = v ; 
    if(mView == null){ 
     LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mView = vi.inflate(id, null); 
    } 
    listView=(ListView)mView.findViewById(R.id.notiID); 
    String sd= yORn.get(position); 
    if(sd=="Y"){ 
     listView.getChildAt(position).setBackgroundColor(Color.GREEN); 
    } 

我也用调试器,我收到条件,如果循环真的,但我的问题是,我没有得到改变的颜色为lisview。 调试器不在if循环中移动。

有什么想法?

+2

不使用'='操作符比较字符串。改用'equals()'。 – dit

+1

从你的getView()方法判断,你首先需要学习如何使用ListView。请观看这第一https://www.youtube.com/watch?v=wDBM6wVEO70 –

回答

-1
@Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
     View mView = v ; 
     ViewHolder holder = null; 
     if(mView == null){ 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(id, null); 
      holder.listItem = mView; 
      mView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)mView.getTag(); 
     } 
     String sd= yORn.get(position); 
     if(sd.equals("Y")){ 
      holder.listItem.setBackgroundColor(Color.GREEN); 
     } 

    } 
    class ViewHolder{ 
     View listItem; 
    } 
+0

'mView.setBackgroundColor(Color.GREEN);'将永远不会被称为... – Selvin

+0

@Selvin是的。你是对的..我只是编辑答案。感谢您的纠正。 –

+0

仍然错误 - 如果视图被重用 - 如果不应该显示backroung将变为绿色 – Selvin

0

您应该使用RecyclerView而不是Listview开始。在适用于您的RecyclerView的适配器中,检查onBindViewHolder中的位置并相应地设置该行的背景颜色。见下文。

@Override 
public void onBindViewHolder(CustomViewHolder holder, int position) { 
    if(position == 2){ 
     holder.ll_item_background.setBackgroundColor(mContext.getResources().getColor(R.color.calc_boxes)); 
    } 

    // Do your stuff here 
} 
0

在此默认为我所提供的背景色为红色,并且如果用户读取,然后像您期望的背景颜色将变为绿色。我编辑了你的代码。

@Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
     View mView = v ; 
     ViewHolder holder = null; 
     if(mView == null){ 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(id, null); 
      holder.listItem = mView; 
      mView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)mView.getTag(); 
     } 

     //By Default 
     holder.listItem.setBackgroundColor(Color.RED); 

     String sd= yORn.get(position); 
     if(sd.equals("Y")){ 
      holder.listItem.setBackgroundColor(Color.GREEN); 
     } 
     } 

    class ViewHolder{ 
     View listItem; 
    } 

希望这是有益:)

相关问题