android中ListView和ListAdapter改变颜色

发布于 2024-11-26 13:37:54 字数 3720 浏览 4 评论 0原文

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

这是我的代码:

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();

}

其他类是: public class EventAdapter extends 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);

    }

}

正如你所看到的,我尝试将文本设置为黄色,但它不起作用,我单击它,它并没有变成黄色。

有解决办法吗?

谢谢

I have a list activity which has a list showing results of a query. Well I want to be able to click on each item and the item changes color but it doesn't work. I want the item to remain selecetd state untill "accepte" button is pressed or item is pressed again. I know that is how text boxes work but i prefer to do it my own way.

Here is my code:

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();

}

and the other classes are:
public class EventAdapter extends 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);

    }

}

As you can see I try to get the text in yellow but it doesn't work I click and it doesn't become yellow.

Is there a solution?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-12-03 13:37:54

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

您需要在 SubSolicitud 模型对象上添加一些内容来指示它已被选择(假设是一个布尔“selected”属性)。

在您的 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);
    }
    // ...
} 

It doesn't work because there is not an EventEntryView for each item in the list - the same EventEntryView is reused to render each.

You need to add something on your SubSolicitud model object to indicate it's been selected (let's say a boolean "selected" property).

In your onItemClicked handler you would toggle this property -

 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
     // ... 
}

(You also need to fix your EventAdapter getItem method to return mEvents.get(position) for this to work...)

Then in your EventAdapter getView method, you use the value of the "selected" property to render the text color -

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);
    }
    // ...
} 
爱格式化 2024-12-03 13:37:54

这就是改变颜色的方法。

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() 内执行此操作,因为每次都会调用它是列表视图上的操作

This is how you change the color.

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);

    }

But if you want to release the item from the color you should iterate through each item of the listview and change it back to normal or you can do it inside the getView() since it is called every time there is action on the listview

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文