1

次のように、アクティビティで文字列のリストを表示します。

    protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    String[] lKeys = new String[mKey.size()];
    int i = 0;
    if (lKeys != null) {
        for (Iterator<String> ite = mKey.iterator(); ite.hasNext();) {
            String element = ite.next();
            if (element.contains("Data")) {
                lKeys[i++] = element;
            } else {
                lKeys[i++] = element + ": " + mValue.get(element);
            }               
        }
    }
    //
    this.setTitle(mTitle);
    setListAdapter(new ArrayAdapter<String>(this,R.layout.popupactivity,lKeys));

このxml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tvResults"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="#000000"
    android:background="#ffffff"
    android:layout_marginLeft="5dp"
    android:textSize="12sp" 
    android:padding="5dp" >
</TextView>

これはうまく機能しますが、リストの行にいくつかの色を付ける必要があります。誰が私を助けることができます?

本当にありがとう!

4

1 に答える 1

1

SimpleAdapter を拡張して getView() メソッドをオーバーライドし、現在のビュー行に背景色を適用することで、ListView の各行の背景に異なる色を追加できます。後でカスタム アダプターをリストとして使用します。

public class CustomList extends SimpleAdapter {
private int[] colors = new int[] { 0x30ADD8E6, 0x30800080 };

public CustomList(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
    super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);
  int colorPos = position % colors.length;
  view.setBackgroundColor(colors[colorPos]);
  return view;
}}

次のステップは、customList アダプターをリストとして使用することです。このような

myList = new CustomList (this,R.layout.lay,R.layout.grid_item,from,target); 
于 2012-04-12T14:04:40.920 に答える