【问题标题】:How to apply conditional formatting to specific item in list which is created through Array如何将条件格式应用于通过数组创建的列表中的特定项目
【发布时间】:2020-01-12 00:22:00
【问题描述】:

我是 Java 和 android 开发的新手。

我有一个列表视图。我想做的是将条件格式(不同背景)应用于特定行。

String[] cars = {"Volvo", "BMW", "Ford", "Mazda","Toyota","VW"};

listView= (ListView)findViewById(R.id.routelistview);
final ArrayList<String>arrayList = new ArrayList<>();

for(int i=0; i< cars.length; i++){
    arrayList.add(cars[i]);
 }

ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
System.out.println("test test test test test");

if (Arrays.asList(listView).contains("Mazda")){
   listView.setBackgroundColor(Color.parseColor("#9fe7ff"));
 }

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(RouteSelection.this, "Clicked Item:"+i+ " "+arrayList.get(i).toString(),Toast.LENGTH_SHORT).show();


    }
});

如果项目是“福特”,我想做的是添加不同的颜色背景

我尝试使用此代码执行此操作,但没有成功。

if (Arrays.asList(listView).contains("Mazda")){
   listView.setBackgroundColor(Color.parseColor("#9fe7ff"));
 }

我的问题是如何检查 arrayAdapter 中的某些 item = "" 并应用条件格式。

【问题讨论】:

    标签: java arrays android-studio android-arrayadapter


    【解决方案1】:

    您可能需要一个自定义适配器来执行此操作,具有列表视图条件格式的自定义适配器示例显示在https://stackoverflow.com/a/57878344/2373819

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      相关资源
      最近更新 更多