【问题标题】:how to change color of list view row after populating using arrayadapter使用arrayadapter填充后如何更改列表视图行的颜色
【发布时间】:2014-08-25 10:23:21
【问题描述】:

我正在创建一个 android 应用程序,我在其中将国家/地区插入到列表视图中,我想更改列表视图顶行的颜色。

我正在使用数组适配器填充列表视图任何人请帮助我如何在填充后为列表视图的第一行着色这是我的代码

 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);

 mListView.setAdapter(mAdapter);

 Collections.reverse(values);

其中“值”是数组列表

现在我正在这样做

public class SpecialAdapter extends ArrayAdapter<String>

{

public SpecialAdapter(Context context, int resource, List<String> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return super.getView(position, convertView, parent);
}

}

 SpecialAdapter ad= new SpecialAdapter(this, R.id.scanning_listView, values);

这是正确的吗?

【问题讨论】:

  • 您需要有一个扩展BaseAdapter 的类,然后覆盖getView() 以实现此目的。
  • aniruddha tm??好的,我试试

标签: android android-listview arraylist android-arrayadapter


【解决方案1】:
@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View view = View.inflate(context, R.layout.item_list, null);

        if (position == 0) {     
           view.setBackgroundColor(Color.BLUE);      //0 (zero), because you want to change the color of first row
        }

        return view;
    }

覆盖 ArrayAdapter 并覆盖那里的 getView 方法。

所以如果你的适配器是这样的:

  public class MyAdapter extends ArrayAdapter

你的 ListActivity 会变成这样:

ArrayAdapter<String> adapter = new MyAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);

检查example

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 2021-05-28
  • 2021-07-31
  • 1970-01-01
  • 2021-05-23
  • 2011-01-03
相关资源
最近更新 更多