2011-08-02 31 views
0

我的愿景是创建一个颜色编码的listView,我希望在每个项目的布局中都有一个细长的颜色条。这个栏应该是基于我传递给活动的int的某种颜色。我如何创建这样一个栏(实质上是一个填充的矩形)并设置其颜色。目前,我正在为我的列表使用自定义布局,并使用带有ArrayList的SimpleAdapter。在Android中创建颜色编码列表视图

我知道我将不得不使用

if (integerForColor == someNumber) 
    //set the color of the shape, bar 

我相信这是我根本无法在此刻想到的,或者必须缺少基本的简单的一件事。预先感谢您的努力。

编辑1

试图解码一些在这里的答案,我意识到,你们需要对我如何建立我的列表我的代码:

public class AddScreen extends Activity implements OnClickListener, 
    OnItemClickListener, OnItemLongClickListener { 
SimpleAdapter adapter; 
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>(); 
ListView listthings; 

public void onCreate(Bundle savedInstanceState) { 
from = new String[] { "row_1", "row_2" }; 
to = new int[] { R.id.row1, R.id.row2 }; 

adapter = new SimpleAdapter(this, painItems, R.layout.mylistlayout, 
      from, to); 

listthings.setAdapter(adapter); 

}

我已添加getView()方法

public View getView (int position, View convertView, ViewGroup parent){ 
    convertView.setBackgroundColor(R.drawable.red); 
    return convertView; 
     //I have no inflater as of now... 
} 

enter image description here 编辑2:根据@huntsfromshadow的建议,我创建了一个新的活动并重写了getView()方法。

//Imports 
public class Adapter extends SimpleAdapter{ 

public Adapter(Context context, List<? extends Map<String, ?>> data, 
     int resource, String[] from, int[] to) { 
    super(context, data, resource, from, to); 
    // TODO Auto-generated constructor stub 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent){ 
    View row = convertView; 
    row.setBackgroundColor(0xFF0000FF); 
    return row; 

} 

} 

不幸的是,它仍然无法正常工作。

回答

0

创建自定义适配器并重写getView。这是您可以根据数值和逻辑做出任何复杂布局决策的关键。

谷歌搜索自定义适配器会给你很多资源。

+0

我仍然不知道如何做到这一点。你能否为这种情况提供一些示例代码? – Kgrover

+0

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/是如何创建自定义阵列适配器的一个很好的例子。数组,游标等基本都是一样的。它们都是通过覆盖所需的适配器的方法来工作的(通常是getView) – huntsfromshadow

+0

你能看看我的更新代码并帮助我吗? – Kgrover

1

假设你拥有一切设置, 它可能最简单的定义XML格式的彩色背景上,像这样(red.xml):

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient 
     android:startColor="#DD0000" 
     android:endColor="#EE0000" 
     android:angle="270" /> 
</shape> 

,然后用它是这样的:

context.getResources().getColor(R.drawable.red). 

您必须传入并保存来自父项Activity的上下文对象。

看我介绍了如何创建一个ListView答案(是的,它从JSON解析数据 - 忽略)here

+0

谢谢你。那么如何将某个listView Item的矩形/条形/形状设置为红色? – Kgrover

+0

查看我提供的链接 - 它提供了完整的说明。 –

+0

我还是有点困惑。你可以看看我更新的代码...我希望这将有助于我如何创建列表,并告诉你如何可以帮助... – Kgrover

0

正如@CaspNZ说,使用的.xml文件形状。

在代码中扩展'ListAdapter'(可能是BaseAdapter或ArrayAdapter)。重写getView函数是这样的:

public View getView (int position, View convertView, ViewGroup parent){ 
    if (convertView == null){ 
     //assuming you already have inflater initilized somewhere 
     convertView = inflater.inflate(you_layout_file.xml, parent, false); 
    } 

    //some logic to determine what color the background should be 
    convertView.setBackgroundDrawable(R.drawable.the_file_you_want_to_use.xml) 
} 
+0

东西仍然不起作用。虽然我还没有添加逻辑,但是请您查看我的更新代码,看看您可以提供哪些帮助? – Kgrover

0

我每个项目的不同文本一起砍死非常相似,颜色代码的东西。您可以很容易地改变它来改变酒吧的颜色。

由于列表中的每个元素都具有不同的颜色,因此您必须记住哪种颜色与阵列中的哪个位置相关联。我只是使用一个简单的整数数组作为我的ColorItemsAdapter的成员。

private class ColorItemsAdapter extends ArrayAdapter<String> 
{ 
    /* 
    * For each line to be added to the log, the text is stored in "items" 
    * and the text color is stored in "colors" 
    */ 
    ArrayList<String> items = new ArrayList<String>(); 
    ArrayList<Integer> colors = new ArrayList<Integer>(); 
    int length = 0; 

    public ColorItemsAdapter(Context context, int textViewResourceId, List<String> stringObj) 
    { 
     super(context, textViewResourceId, stringObj); 
     if(stringObj != null) 
     { 
      length = stringObj.size(); 
     } 
    } 

    /** 
    * Adds an item to the adapter, and specifies what color the item should be 
    * @param item 
    * The text to be added 
    * @param color 
    * The color of the text to be added 
    */ 
    public void add(String item, Integer color) 
    { 
     items.add(item); 
     colors.add(color); 
     length++; 
     super.add(item); 
    } 

    /** 
    * Adds an item to the adapter at position <i>index</i> 
    * @param item 
    * The text to be inserted 
    * @param index 
    * The index at which to insert the text 
    * @param color 
    * The color of the text to be inserted 
    */ 
    public void insert(String item, int index, Integer color) 
    { 
     items.add(index, item); 
     colors.add(index, color); 
     length++; 
     super.insert(item, index); 
    } 

    /* 
    * Changes the color of the TextView appropriately before calling the super 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView v = (TextView) convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = (TextView) vi.inflate(R.layout.log_list_item, null); 
     } 

     /* 
     * Here I am setting the text color 
     * but you could set some other attribute instead. 
     */ 
     v.setTextColor(colors.get(position)); 

     return super.getView(position, v, parent); 
    } 
} 

然后,将项目添加到列表(在这种情况下,一个红色的),你可以只使用

myColorItemsAdapter.insert(message, 0, Color.RED);