android中的ListView和列表适配器更改颜色

android中的ListView和列表适配器更改颜色,android,listview,text,colors,adapter,Android,Listview,Text,Colors,Adapter,我有一个列表活动,它有一个显示查询结果的列表。嗯,我想能够点击每个项目,项目改变颜色,但它不工作。我希望项目保持selecetd状态,直到按下“Accept”(接受)按钮或再次按下项目。我知道文本框就是这样工作的,但我更喜欢用我自己的方式 这是我的密码: public void createList() { if (ok == 1) { //hay muachas possibilidades if (sol.get(i).getMultiseleccion() !=

我有一个列表活动,它有一个显示查询结果的列表。嗯,我想能够点击每个项目,项目改变颜色,但它不工作。我希望项目保持selecetd状态,直到按下“Accept”(接受)按钮或再次按下项目。我知道文本框就是这样工作的,但我更喜欢用我自己的方式

这是我的密码:

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 void createList(){
如果(ok==1){
//可能的干草
if(sol.get(i).getMultiseleccion()!=0){
bt2.设置可见性(视图不可见);
}否则{
//索洛·斯克里卡·恩乌纳
//lv.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
bt2.设置可见性(View.VISIBLE);
}
字符串hd1=sol.get(i.getDescarcoud();
tv2.setText(hd1);
ArrayList sub=sol.get(i).getSubSol();
mAdapter=新的事件适配器(此,子);
setListAdapter(mAdapter);
lv.setTextFilterEnabled(真);
lv.computescoll();
lv.设置分隔灯(1);
lv.setItemsCanFocus(假);
lv.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0、视图arg1、,
整数位置,长arg3){
ok=1;
//OnListClick(位置,arg1);
if(sol.get(i).getMultiseleccion()!=0){
//多重响应
((EventEntryView)arg1.text1.setTextColor(Color.YELLOW);
瓜达尔雷普斯塔();
}否则{
buscarElementos();
}
}
});
}
//错误信息
其他的
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);

    }

}
public ArrayList mEvents=null;
公共事件适配器(上下文c、ArrayList下层){
mContext=c;
mEvents=底土;
}
public int getCount(){
返回mEvents.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
EventEntryView btv;
if(convertView==null){
btv=neweventryview(mContext,mEvents.get(position));
}否则{
btv=(EventEntryView)convertView;
字符串title1=mEvents.get(position.getDescription();
如果(标题1!=null){
btv.Settext1标题(标题1);
}
}
btv.setBackgroundColor(颜色:黑色);
返回btv;
}
私有上下文;
公共事件{
mEvents.clear();
notifyDataSetChanged();
}
公共无效添加事件(次合法e){
添加(e);
}
}
公共类EventEntryView扩展了LinearLayout{
//私人观点膨胀观点;
私有文本视图文本1;
//私有文本视图文本2;
public EventEntryView(上下文上下文,SubSolicitud SubSolicitud){
超级(上下文);
此设置方向(垂直);
text1=新的TextView(上下文);
text1.setTextSize(20);
text1.设置填充(10,10,10,10);
text1.setTextColor(Color.WHITE);
字符串t=subSolicitud.getDescription();
text1.setText(t);
addView(文本1,新的LinearLayout.LayoutParams(
LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
}
公共void setText1Title(字符串title1){
//TODO自动生成的方法存根
text1.setText(标题1);
}
}
正如你所看到的,我试图得到黄色的文本,但它不工作,我点击,它没有变成黄色

有解决办法吗


谢谢

这就是你改变颜色的方法

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

    }
public单击(AdapterView arg0,视图arg1,
整数位置,长arg3){
position=position-listView.getFirstVisibleItem();
((EditText)arg0.getChildAt(position).findViewById(R.id.myTextView)).setTextColor(Color.YELLOW);
}

但是,如果要从颜色中释放该项,则应该遍历listview的每个项并将其更改回正常状态,或者可以在
getView()中执行此操作
由于每次listview上有操作时都会调用它,因此它不起作用,因为列表中的每个项目都没有
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
     // ... 
}

这个问题已经在这里得到了回答:谢谢,但这不是我想要的。他要参加另一项活动,但我不知道,他有android:choiceMode=“singleChoice”,但我不能选择几个项目。thaks很多你刚刚救了我一天这个方法很有效,而且它解决了我遇到的另一个问题!
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);
    }
    // ...
}