Android ListView适配器"已填充"打回来

时间:2017-09-01 01:58:00

标签: android

我使用SimpleAdapter填充ListView。执行此操作后,我立即尝试循环子视图以编程方式设置其背景颜色。问题是,在调用listView.setAdapter()之后,ListView可能没有任何子节点。我不知道哪个回调可以坚持我的功能。

        // A HashMap to store the values for the ListView rows
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

    for (int i = 0; i < steps.length; i++) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("txt", steps[i]);
        hm.put("icon", Integer.toString(step_images[i]));
        aList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = {"icon", "txt"};

    // Ids of views in layout
    int[] to = {R.id.icon, R.id.txt};

    // Instantiating an adapter to store each items
    // R.layout.listview_steps defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_steps, from, to);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);



    //fixme after the listView adapter is set, the listView may not have any children immediately.
    //I'm not sure which callback to use to properly call refreshStepsListView().. So I'm hacking it and I just wait 250ms
    (new Handler())
            .postDelayed(
                    new Runnable() {
                        public void run() {
                            refreshStepsListView();
                        }
                    }, 250);

3 个答案:

答案 0 :(得分:1)

不需要回电。活它

public class ColoredAdapter extends SimpleAdapter {

     ColoredAdapter(...) {
         super(context, list,  reslayout, from, to);
    } 

    getView(...) {
        // set colors here 
   } 

或者您可以创建Step类而非Hashmap的列表,并使用ArrayAdapter<Step>

答案 1 :(得分:1)

据我了解。您只想更改列表视图中每个项目的背景。

首先,为Adapter定义类项。

    public class Item {
      private String mTitle;
      private String mBackgroundColor;

      public void setTittle(String title){
        this.mTitle = title;
      }

      public void setBackgroundColor(int color){
        this.mBackgroundColor= color;
      }

      public void getTitle (){
        return this.mTitle;
      }


      public void getBackgroundColor(){
        return this.mBackgroundColor;
      }
   }

然后使用ArrayAdapter代替SimpleAdapter 请参阅此示例Here

确保在getView()的{​​{1}}内,您已设置ArrayAdapter的项目背景颜色

item.getBackgroundColor()

如果要更改背景颜色,只需将新颜色设置为itemView.setBackgroundResource(item.getBackgroundColor()); 项,然后调用item.setBackgroundColor(newColor)刷新适配器。

答案 2 :(得分:0)

在适配器内进行背景着色。您可以简单地扩展SimpleAdapter并在创建视图时访问这些视图。在适配器中,您需要执行任何视图操作逻辑。

不建议对子项进行迭代,否则会导致错误,代码难以维护以及性能不佳。

public class ColorAdapter extends SimpleAdapter {

    public ColorAdapter(Context context, 
                        List<? extends Map<String, ?>> data, 
                        int resource, 
                        String[] from, 
                        int[] to) {

        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        view.setBackgroundColor(Color.RED);
        return view;
    }
}