【问题标题】:Colouring a ListView in oncreate在 oncreate 中为 ListView 着色
【发布时间】:2013-05-23 19:13:06
【问题描述】:

我有一个 ListView,我想根据 ArrayList<Integer> 如果项目的位置存在于数组中,则该项目的背景将为绿色,否则应为红色。我使用SetListColor 来做到这一点,但它不起作用。

public class createtarget extends ListActivity
{

         String [] Target;
    ListView lstView;
    public static ArrayList<Integer> coloredItems;


    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.createtarget);

        coloredItems = new ArrayList<Integer>();
        coloredItems.add(1);

        lstView = getListView();    
        lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
        lstView.setTextFilterEnabled(true);

        SetListColor(this.findViewById(android.R.id.content)); // Get View of ListView

        Target=new String []{"A","B","C"};

        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, Target));

    }


public void SetListColor(View v)
     {
        for(int i=0;i<lstView.getCount();i++)
        {   
            System.out.println("Item is: "+i);
         if(createtarget.coloredItems.contains(i))
                    v.setBackgroundColor(Color.GREEN);
         else
             v.setBackgroundColor(Color.Red); 
        }
     }

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    要更改列表项,您需要对 ListAdapter 进行操作,而不是对整个 List 进行操作。

    只需扩展您的 ArrayAdapter 并覆盖该方法

    public View getView (int position, View convertView, ViewGroup parent)

    在返回之前更改视图的颜色(使用位置找出什么是什么)

    该方法的代码应该看起来更像这样(这会根据单元格的位置(偶数或奇数)改变颜色):

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        if(position%2 == 0)
            v.setBackgroundColor(Color.RED);        
        else
            v.setBackgroundColor(Color.WHITE);
        return v;
    }
    

    注意:此代码未经测试

    希望对你有帮助

    EDIT:更正了对 getView 的调用以应用于 super。向指出这一点的问题的发布者致敬(不知何故,他的编辑被拒绝了,不应该被拒绝)。

    【讨论】:

      【解决方案2】:

      我不确定在 listview 项目上应用颜色的样式,但是一种编写自定义适配器和设置 rowView BackgroundColor 之类的通用方法。

      public View getView(int position, View v, ViewGroup parent) {
      
      
              if(v!= null)
              {
               for(int i=0;i<lstView.getCount();i++){
                 if(createtarget.coloredItems.contains(i))
                      v.setBackgroundColor(Color.GREEN);
                 }else{
                      v.setBackgroundColor(Color.Red); 
                 }
               }
              }           
         return v;
      }
      

      【讨论】:

        【解决方案3】:

        使用Base Adepter 而不是修复视图运行时。

        并参考那个问题

        Android : Alternate colors in ListView using ArrayAdapter

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-17
          • 2012-12-21
          • 2021-04-21
          相关资源
          最近更新 更多