2017-09-01 108 views
1

我使用SimpleAdapter填充ListView。这样做后,我有一个函数,试图循环子视图以编程方式设置其背景颜色。问题是在调用listView.setAdapter()之后,ListView可能没有任何儿童。我不知道是哪个回调来粘贴我的功能。Android 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); 
+1

你需要在适配器上实现getView –

回答

1

不需要回调。不要住在一起

public class ColoredAdapter extends SimpleAdapter { 

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

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

或者你可以创建一个Step类,而不是一个HashMap的名单,加上使用ArrayAdapter<Step>

+0

谢谢!这应该工作。颜色由另一个模型中的状态标志驱动,但我会解决这个问题。我是Android新手。一般来说,我需要花更多的时间在适配器上。 – voodoodrul

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; 
    } 
} 
1

从我的理解。你只是想改变列表视图中每个项目的背景。

首先,定义适配器的类项目。

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

确保里面你ArrayAdapter,您已设置项目的背景颜色的getView()item.getBackgroundColor()

itemView.setBackgroundResource(item.getBackgroundColor()); 

当你想改变背景颜色,您可以将新颜色设置为item.setBackgroundColor(newColor)并致电adapter.notifyDataSetChanged()刷新适配器。

+0

确实。这基本上是我去的方式,它运作良好。 – voodoodrul

+0

很高兴听到:) –