【问题标题】:ListView and List Adapter in android change colorandroid中的ListView和List Adapter改变颜色
【发布时间】:2011-10-13 13:04:26
【问题描述】:

我有一个列表活动,其中有一个显示查询结果的列表。好吧,我希望能够单击每个项目并且项目会更改颜色,但它不起作用。我希望该项目保持选择状态,直到按下“接受”按钮或再次按下项目。我知道这就是文本框的工作方式,但我更喜欢以我自己的方式。

这是我的代码:

public void createList() {

    if (ok == 1) {
    //hay muachas possibilidades
    if (sol.get(i).getMultiseleccion() != 0){

        bt2.setVisibility(View.INVISIBLE);
    }else {
        //solo se clika en una
        //lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        bt2.setVisibility(View.VISIBLE);
    }

        String hd1 = sol.get(i).getDescSolicitud();

        tv2.setText(hd1);

        ArrayList<SubSolicitud> sub = sol.get(i).getSubSol();
        mAdapter = new EventAdapter(this, sub);
        setListAdapter(mAdapter);
        lv.setTextFilterEnabled(true);
        lv.computeScroll();
        lv.setDividerHeight(1);
        lv.setItemsCanFocus(false);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                    ok = 1;
                    //OnListClick(position, arg1);
                    if (sol.get(i).getMultiseleccion() != 0) {
                        // multiples respuestas

                                ((EventEntryView)arg1).text1.setTextColor(Color.YELLOW);

                        guardarRespuesta();
                    }else {
                    buscarElementos();
                    }

            }

        });
    }

    // informar el usuario de que hay un error
    else
        buildAlertDialog();

}

其他类是: 公共类 EventAdapter 扩展 BaseAdapter {

    public ArrayList<SubSolicitud> mEvents = null;

    public EventAdapter(Context c, ArrayList<SubSolicitud> subsol) {
        mContext = c;
        mEvents = subsol;
    }

    public int getCount() {
        return mEvents.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        EventEntryView btv;
        if (convertView == null) {
            btv = new EventEntryView(mContext, mEvents.get(position));

        } else {
            btv = (EventEntryView) convertView;
            String title1 = mEvents.get(position).getDescripcion();

            if (title1 != null) {
                btv.setText1Title(title1);
            }

        }
        btv.setBackgroundColor(Color.BLACK);

        return btv;

    }

    private Context mContext;

    public void clearEvents() {
        mEvents.clear();
        notifyDataSetChanged();
    }

    public void addEvent(SubSolicitud e) {
        mEvents.add(e);

    }

}

public class EventEntryView extends LinearLayout {

    // private View inflatedView;
    private TextView text1;

    // private TextView text2;

    public EventEntryView(Context context, SubSolicitud subSolicitud) {
        super(context);
        this.setOrientation(VERTICAL);



        text1=new TextView(context);
        text1.setTextSize(20);
        text1.setPadding(10, 10, 10, 10);
        text1.setTextColor(Color.WHITE);
        String t = subSolicitud.getDescripcion();
        text1.setText(t);

        addView(text1, new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    }

    public void setText1Title(String title1) {
        // TODO Auto-generated method stub
        text1.setText(title1);

    }

}

如您所见,我尝试将文本显示为黄色,但它不起作用,我单击它并没有变成黄色。

有解决办法吗?

谢谢

【问题讨论】:

  • 这里已经回答了这个问题:stackoverflow.com/questions/6731167/…
  • 谢谢,但这不是我想要的。他要去参加另一个活动,但我不是,他有 android:choiceMode="singleChoice" 但我不能选择几个项目。

标签: android listview text colors adapter


【解决方案1】:

它不起作用,因为列表中的每个项目都没有 EventEntryView - 相同的 EventEntryView 被重复用于呈现每个项目。

您需要在您的 SubSolicitud 模型对象上添加一些内容以表明它已被选中(假设是布尔“选定”属性)。

在您的 onItemClicked 处理程序中,您将切换此属性 -

 public void onItemClick(AdapterView<?> adapterView, View view,
                int position, long id) {
     // ...
     SubSolicitud selectedSubSol = (SubSolicitud)adapterView.getAdapter().getItem(id);
     boolean currentValue = selectedSubSol.isSelected();
     selectedSubSol.setSelected(!currentValue); // toggle 'selected' on and off
     // ... 
}

(您还需要修复您的 EventAdapter getItem 方法以返回 mEvents.get(position) 以使其正常工作...)

然后在您的EventAdapter getView 方法中,您使用“selected”属性的值来呈现文本颜色-

public View getView(int position, View convertView, ViewGroup parent) {
    // ...
    if (mEvents.get(position).isSelected()) {
        btv.text1.setTextColor(Color.YELLOW);
    } else {
        // you have to have an else to set it back to the default
        // color, because the view is reused for all list items.
        btv.text1.setTextColor(Color.WHITE);
    }
    // ...
} 

【讨论】:

  • 感谢您为我节省了很多时间,这种方法效果很好,而且它解决了我遇到的另一个问题!
【解决方案2】:

这就是你改变颜色的方式。

public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {

                       position = position - listView.getFirstVisibleItem();
                       ((EditText)arg0.getChildAt(position).findViewById(R.id.myTextView)).setTextColor(Color.YELLOW);

    }

但是,如果您想从颜色中释放项目,则应该遍历列表视图的每个项目并将其更改回正常状态,或者您可以在 getView() 内执行此操作,因为每次对列表视图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多