在customadapter android中为行着色

发布于 2024-12-27 00:50:49 字数 898 浏览 5 评论 0原文

好的,到目前为止我已经创建了这个,并且我的项目正在显示。假设我在一个数组中包含与行一样多的元素的键值。如果我有 16 行,那么我的数组就有 16 个元素。我想检查 array[0] 并根据值 setColor 到第一行,然后检查 array[1] 并 setColor 到下一行,依此类推。谁能帮我:

public class SimpleAdapter extends ArrayAdapter<String>{

private final Context context;



private String data[] = null;

    public SimpleAdapter(Context context,  String[] data) {
        super(context, R.layout.row, data);
        this.context = context;
        this.data = data;
    }



  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1);
        textView.setText(data[position]);
        return rowView;
    }
}

Ok so far I have created this, and my items are being displayed. Lets suppose that I am having the key values in an array with as many elements as the rows. If I have 16 rows, then my array has 16 elements. I want to check the array[0] and depending on the value setColor to first row, then check array[1] and setColor to next row and so on. Can anyone help me:

public class SimpleAdapter extends ArrayAdapter<String>{

private final Context context;



private String data[] = null;

    public SimpleAdapter(Context context,  String[] data) {
        super(context, R.layout.row, data);
        this.context = context;
        this.data = data;
    }



  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1);
        textView.setText(data[position]);
        return rowView;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2025-01-03 00:50:49

在 getView() 中你可以有类似的东西:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);

switch(position) {
  case 0:
   rowView.setBackgroundColor(color0)
   break;
  case 1:
   rowView.setBackgroundColor(color1)
   break;
...
}

return rowView;

In getView() you can have something like:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);

switch(position) {
  case 0:
   rowView.setBackgroundColor(color0)
   break;
  case 1:
   rowView.setBackgroundColor(color1)
   break;
...
}

return rowView;
木落 2025-01-03 00:50:49

在数组中定义颜色/(背景可绘制)非常简单。就像

int[] color = {your colors };

然后使用下面的行

int mycolor = position % color.length;
rowView.setBackground/color = color[mycolor];
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);
return rowView;

It is simple define your color/(background drawable) in an array. like

int[] color = {your colors };

then just use the below lines

int mycolor = position % color.length;
rowView.setBackground/color = color[mycolor];
TextView textView = (TextView) rowView.findViewById(R.id.text1);
textView.setText(data[position]);
return rowView;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文