2016-12-29 73 views
0

我期待在getView()中设置所选项目,但正在工作,但是我的列表中的每个项目都被选中。我已用吐司进行过测试,并显示正确的条件以便工作。条件检查以查看特定项目的数据库条目是否设置为true(因此被选中)。getView ListView所选项目颜色变化

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    if(isItemSelected.equals("true")){ 
      listviewTitles.setBackgroundColor(0xAAAAFFFF); 
    } 
    else if (isItemSelected.equals("false")){ 
    // Default color  
    } 
} 
+1

没有你所写的listviewTitles.setBackgroundColor(非选定的颜色);在其他部分 –

+0

你想突出显示一个特定的单元格?如果是这样的话,你也可以设置你的单元布局的背景,像这样linearLayout.setBackgroundResource(COLOR.GREY); –

+0

请将整个代码放在getView()中---检查您是否正在回收列表中的视图,因为如果您选择的项目在回收时已被选中!如果多数民众赞成什么发生! – shadygoneinsane

回答

1

你应该更新你的背景颜色,像下面的每个条件;

listviewTitles.setBackgroundColor(isItemSelected.equals("true") ? selectedColor : unSelectedColor); 
+0

Didnt for me – user3175140

0

试试这个,并在代码中进行这些更改,希望它能够解决。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) {  
     if (isItemSelected.equals("true")){ 
listviewTitles.setBackgroundColor(0xAAAAFFFF); 
      } 
      else if (isItemSelected.equals("false")){ 
      // Default color  
      } 
      }else{ 

     if (isItemSelected.equals("true")){ 

    listviewTitles.setBackgroundColor(0xAAAAFFFF); 
      } 
      else if (isItemSelected.equals("false")){ 
      // Default color  
      } 
     } 

请尽量将

+0

Didnt for me – user3175140

0

我相信我对你的需求的解决方案,

在视图文件中的代码粘贴

SparseBooleanArray singleChecked; 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     if (position != singleListSelectedPosition) { 
      singleListSelectedPosition = position; 
      int totalCount = lvSingleSelect.getCount(); 
      for (int i = 0; i < totalCount; i++) { 
       if (i == position) { 
        boolean stat = singleChecked.get(position, false); 
        singleChecked.put(position, !stat); 
       } else { 
        singleChecked.put(i, true); 
       } 
      } 
      adapter.setChecked(singleChecked); 
     } 
    } 

这是您的适配器类代码:

public void setChecked(SparseBooleanArray ch) { 
     singleChecked = ch; 
     notifyDataSetChanged(); 
} 

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

    if (singleChecked.get(position, false)) { 
     convertView.setBackgroundColor(getResources() 
        .getColor(R.color.titlebar_background_color)); 
    } else { 
     convertView.setBackgroundColor(
        getResources().getColor(R.color.emphasis_color)); 
    } 

请让我知道如果你有任何麻烦,总是乐于帮助。

+0

Didnt for me – user3175140

+0

同样的问题你越来越??,让我知道你是用这个代码得到什么问题。 – Bethan

0

我想你应该尝试添加一个额外的值,如布尔值,同时在你的数组列表中添加数据。对于选中为true,对于未选中为false。最初为所有添加false。然后当你点击listviewTitles

int positionClicked; 

listviewTitles.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      for (int i = 0; i < bean_List.size(); i++) { 
      if (i == position) { 
       positionClicked = position; 
       bean_List.get(position).setIsClicked(true); 
       notifyDataSetChanged(); 
       //Do your task here... 
      } else { 
       bean_List.get(i).setIsClicked(false); 
       notifyDataSetChanged(); 
      } 
    } 
}); 

而且在getView()使用此: -

if (bean_List.get(position).getIsClicked() == true) { 
     listviewTitles.setBackgroundColor(0xAAAAFFFF); 
     //change color accordingly 
    } else { 
     listviewTitles.setBackgroundColor(0xAAAAFFFF); 
     //change color accordingly 
    } 
+0

没有为我工作 – user3175140

相关问题