0

私は(ここのどこかでアイデアを得た)Listviewで作られた2つの列で構成されている(AlertDialogに表示されている)を持っています。HashMap各列はTextView. それはうまくいきます。

特定の行の最初の列のテキストカラーを変更したいのですが、これを選択して設定する方法がわかりません...何時間もグーグル検索しました! 手がかりはありますか??

これは私のコードです(listsはですSortedSet):

public void showlistdesc () {
    ListView listview = new ListView(this);
    listview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    listview.setSoundEffectsEnabled(true);
    listview.setSelector(R.drawable.selector);

    Integer i=0, pos = 0;
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map;
    for (String l : lists) {
        if (l.equals(currlist)) pos = i;
        i++;
        map = new HashMap<String, String>();
        map.put("list", l);
        map.put("desc", getlistdesc(l, false));
        mylist.add(map);
    }

    listview.setAdapter(new SimpleAdapter(this, mylist, R.layout.lists_row,
                new String[] {"list", "desc"}, new int[] {R.id.list1, R.id.desc1}));

    AlertDialog.Builder builder = new AlertDialog.Builder(AveActivity.this)
            .setView(listview)
            .setIcon(R.drawable.ic_launcher)
            .setTitle(lists.size() + " bird lists in databases");

    final AlertDialog dial = builder.create();

    //Scrolls the listview to this position at top
    listview.setSelection(pos);

    dial.show();
}

ありがとう!

編集

次のコードで拡張しようとしましSimpleAdapterたが、私が見ることができるのは黒一色の行だけです:

public class MySimpleAdapter extends SimpleAdapter {
private Context context;

public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data,
        int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.lists_row, null);
    }

    TextView list1 = ((TextView) v.findViewById(R.id.list1));
    TextView desc1 = ((TextView) v.findViewById(R.id.desc1));

    if (v.isPressed()) {
        v.setBackgroundColor(context.getResources().getColor(R.color.pressed));
        list1.setTypeface(null, 1);
        list1.setTextColor(context.getResources().getColor(R.color.selected));
    } else {
        v.setBackgroundColor(context.getResources().getColor(R.color.black));
        if (v.isSelected()) {
            list1.setTypeface(null, 1);
            list1.setTextColor(context.getResources().getColor(R.color.checked));
            desc1.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            list1.setTypeface(null, 0);
            list1.setTextColor(context.getResources().getColor(R.color.normal));
            desc1.setTextColor(context.getResources().getColor(R.color.lesswhite));
        }
    }

    return v;
}
}
4

2 に答える 2

0

今、特別な要件があります。それはもはや単純SimpleAdapterではありません。求めている効果を達成するために使用することはできません。拡張する必要がありBaseAdaptergetView()メソッドで次のことができます。

if (position == 0) {
    convertView.setBackground(color1);
} else {
    convertView.setBackground(color2);        
}

または、必要なものがHeaderListView.addHeaderView()である場合は、それほど手間をかけずに使用できます。

于 2013-05-20T02:24:36.067 に答える
0

ここでいくつかの投稿に本質的に含まれている解決策を見つけました。私がしたことはlistview.setItemChecked(pos, true);、以前に試して失敗したものを使用することでした。何も表示されなかったからです。問題は、それLinearLayoutが実装されていないことでしたCheckable。だから私はこれを実装しました(コードは本質的にここのどこかにあります):

public class CheckableLinearLayout extends LinearLayout implements Checkable {
private boolean isChecked;

public CheckableLinearLayout(Context context) {
    super(context);
}

public CheckableLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setChecked(boolean isChecked) {
    this.isChecked = isChecked;
    TextView list1 = ((TextView) findViewById(R.id.list1));
    TextView desc1 = ((TextView) findViewById(R.id.desc1));
    if (isPressed()) {
        list1.setTextColor(getResources().getColor(R.color.selected));
        setBackgroundColor(getResources().getColor(R.color.pressed));
    } else {
        setBackgroundColor(getResources().getColor(R.color.black));
        if (isChecked) {
            list1.setTextColor(getResources().getColor(R.color.checked));
            desc1.setTextColor(getResources().getColor(R.color.white));
        } else {
            list1.setTextColor(getResources().getColor(R.color.normal));
            desc1.setTextColor(getResources().getColor(R.color.lesswhite));
        }
    }
}

public boolean isChecked() {
    return isChecked;
}

public void toggle() {
    setChecked(!isChecked);
}
}

以上です。を使用する必要selectorも、アダプターに触れる必要もありません。これを機能させるには、行で通常の の代わりにlayout使用します。<com.[yourpackage].CheckableLinearLayoutLinearLayout

于 2013-05-21T02:49:31.510 に答える