【问题标题】:How do I change the color of a row in ListView depending on what is on each row?如何根据每行的内容更改 ListView 中一行的颜色?
【发布时间】:2017-05-05 20:13:13
【问题描述】:

我在 Android 的 SolutionActivity 类中有一个列表视图。适配器根据用户在另一个活动中输入的结果用元素填充列表。列表视图只能有两个值,“真”或“假”。我在整个网络上进行了搜索,我看到在自定义适配器类中调用了很多“getView”方法,但是我试图实现它,但我做不到弄清楚怎么做?我是为我的适配器创建一个单独的类吗?或者我可以将它添加到我的 SolutionActivity 的末尾吗?以及如何使用 getView 方法?无论如何这是我的代码...

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()”的值为真,我想将行设置为绿色,如果为假,则设置为红色。任何人都可以提出一个好的解决方案吗?谢谢

【问题讨论】:

  • 您必须为此使用自定义适配器
  • @AliAhsan 是的,我知道,我只是不知道如何像我在问题中所说的那样实现它。
  • 好的,让我在答案中添加代码

标签: java android arrays listview android-arrayadapter


【解决方案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);

你很高兴:)

【讨论】:

  • 如果您还有任何问题,请告诉我
  • 我现在要测试一下,抱歉回复晚了
  • 您好,我在您提供的构造函数中收到“ArrayAdapter 中没有默认构造函数”错误
  • “this.context”在 customadapter 类中也无效
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多