0

我有一个Listview(在 AlertDialog 中显示),它由两列组成,由 a 组成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

我找到了一个解决方案,基本上包含在这里的几个帖子中。我所做的是使用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,也无需触摸适配器。为了使这项工作,在 rowlayout中,使用<com.[yourpackage].CheckableLinearLayout而不是通常的LinearLayout.

于 2013-05-21T02:49:31.510 回答
0

现在你有特殊要求,它不再简单,你不能用它SimpleAdapter来达到你要求的效果。您将不得不扩展BaseAdapter并且在getView()方法中您可以执行以下操作:

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

或者,如果您想要的是Header,则可以轻松使用ListView.addHeaderView()

于 2013-05-20T02:24:36.067 回答