2011-08-02 8 views
0

私の考えは、色分けされたリストビューを作成して、各アイテムのレイアウトに細い色のバーを配置したいと思うことです。このバーは、アクティビティに渡すintに基づいて特定の色にする必要があります。そのような棒(基本的に塗りつぶし矩形)を作成し、その色を設定するにはどうすればいいですか?現在、私は自分のリストにカスタムレイアウトを使用しており、SimpleAdapterとArrayListを使用しています。Androidで色分けされたListViewを作成する

私はこれは私が単に現時点で考えることができない、または基本的に不足しているしなければならない単純なことであると確信しています

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

を使用する必要があります知っています。あなたの努力のために事前にありがとうございます。

EDIT 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 EDIT 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をオーバーライドします。これは、値とロジックに基づいて複雑なレイアウト決定を行うことができるポイントです。

カスタムアダプターをGoogle検索すると、多くのリソースが得られます。

+0

を使用することができます。このシナリオのサンプルコードを提供していただけますか? – Kgrover

+0

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/カスタムアレイアダプタを作成する方法の良い例です。基本は配列、カーソルなどと同じです。必要なアダプタ(通常はgetView)のメソッドをオーバーライドすることですべて動作します – huntsfromshadow

+0

あなたは私の更新されたコードを見て助けてくれますか? – Kgrover

1

それはそう(red.xml)のような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アイテムの矩形/バー/シェイプを赤色に設定するのですか? – 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); 
    } 
} 

、(この場合は赤1)をリストに項目を追加するには、あなただけの私はまだこれを行う方法について確認していない

myColorItemsAdapter.insert(message, 0, Color.RED); 
関連する問題