如何根据每行上的内容更改ListView中某一行的颜色?

问题描述:

我在Android的SolutionActivity类中有一个listview。适配器使用基于用户在其他活动中输入的结果的元素填充列表。 listview只能有两个值,“true”或者“false”。我在网上搜索了很多'getView'方法,在自定义的适配器类中调用,但是我试图实现这个,但是我不能弄清楚如何?难道我创建一个独立的类,只是为了我的适配器?或者,我可以在我的SolutionActivity的末尾添加呢?我如何使用getView方法?反正这是我的代码...如何根据每行上的内容更改ListView中某一行的颜色?

public void setUserResults() { //displays the bit combination and users services in the listviews 
    ListView serviceNames = (ListView) findViewById(R.id.listofservices); 
    ListView bitResults = (ListView) findViewById(R.id.bitresults); 
    UserInputSet userInputSet = UserInputSet.getInstance(); 
    List<String> userServices = MainActivity.dimensions; 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
      userServices); 
    ArrayAdapter<Boolean> bitArrayAdapter = new ArrayAdapter<Boolean>(this, android.R.layout.simple_list_item_1, 
      CustomUseCase.getBestComboArray()); 
    serviceNames.setAdapter(arrayAdapter); 
    bitResults.setAdapter(bitArrayAdapter); 
} 

我想设置的行绿色如果“getBestComboArray()”的值是true,否则红色如果是假的。任何人都可以提出一个很好的解决了这个?谢谢

+0

你必须使用自定义适配器 –

+0

@AliAhsan你知道,我只是不知道如何实现它,就像我在问题中所说的那样。 –

+1

好的,让我在回答中添加代码 –

创建自定义适配器类

public class CustomAdapter extends ArrayAdapter<String> { 
private List<Boolean> greenColor; 
private List<String> Words; 

public CustomAdapter(Activity context, List<String> words,List<Boolean> layoutcolor) 
{ 
        Words = words; 
      this.context = context 
      greenColor = layoutcolor; 

} 

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

    View view = convertView; 
    if(view==null) 
     view = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false); 


    View view1 = view.findViewById(R.id.colorContainer); 

    if(layoutColor.get(position)){ 
    view1.setBackgroundColor(Color.GREEN); 
    }else{ 
      view1.setBackgroundColor(Color.RED); 
      } 


    TextView textViewdefault = (TextView) view.findViewById(R.id.text_view); 
    textViewdefault.setText(words.get(position)); 

    return view; 

} 
} 

创建名为LIST_ITEM布局和享有改变你想显示每行

<LinearLayout 
    android:id="@+id/colorContainer" 
    android:layout_width="match_parent" 
    android:layout_toRightOf="@id/image" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 

    > 
<TextView 
    android:id="@+id/text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:text="text1" 
    android:textSize="18sp" 
    android:paddingTop="16dp" 
    android:layout_marginLeft="16dp" 
    android:textColor="#ffffff" 
    /> 



</LinearLayout> 

然后设置调用适配器列表视图

CustomAdapter <String> bitArrayAdapter = new CustomAdapter <String>(this, TextViewList, // TextViewList is the list of text for textview of each row 
CustomUseCase.getBestComboArray()); 

bitResults.setAdapter(bitArrayAdapter); 

,你是好去:)

+0

让我知道如果你仍然有任何问题 –

+0

我现在要测试,对于迟到的回复感到抱歉 –

+0

嗨,我得到“ArrayAdapter 没有默认构造函数”错误你给的构造函数 –