Java 如何在列表视图中保留所选项目的背景色?

Java 如何在列表视图中保留所选项目的背景色?,java,android,Java,Android,我正在活动中使用listview和edittext。关于项目的选择 在列表视图中,我可以使用 “android:listselector”。现在,我开始编辑文本。在我开始编辑时立即 文本列表视图中所选项目的背景色更改为 违约如何预防 <ListView android:layout_width="wrap_content" android:layout_height="129dp" android:id="@+id/listView2" android:ch

我正在活动中使用listview和edittext。关于项目的选择 在列表视图中,我可以使用 “android:listselector”。现在,我开始编辑文本。在我开始编辑时立即 文本列表视图中所选项目的背景色更改为 违约如何预防

<ListView
    android:layout_width="wrap_content"
    android:layout_height="129dp"
    android:id="@+id/listView2"
    android:choiceMode="singleChoice"
    android:listSelector="#666666"
    android:layout_below="@+id/button7"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
   android:layout_marginTop="34dp" />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:padding="-11dp"
    android:width="300dp"
    android:inputType="number"
    android:textSize="34sp"
    android:editable="true"
    android:layout_below="@+id/listView2"
    android:textStyle="bold"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="163dp"
    android:hint="$" />

公共类类别\u适配器扩展BaseAdapter{
私人语境;
私人充气机;
//初始化所选位置
private int selectedPosition=-1;
公共类别_适配器(上下文、数组列表项、,
(弦乐表演){
this.context=上下文;
this.inflater=(LayoutInflater)上下文
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
//TODO自动生成的方法存根
如果(视图==null){
视图=充气机。充气(R.layout.category\u titles\u n\u sellers\u list,
父母,假);
//将背景设置为所选位置
如果(位置==selectedPosition){
view.setBackgroundColor(context.getResources().getColor(
R.color.orange_列表);
//view.setBackgroundColor(Color.parseColor(#FFA500”);
}否则{
view.setBackgroundColor(context.getResources().getColor(
R.color.white);
//视图.setBackgroundResource(R.color.white);
}
返回视图;
}
//从activtiy获取所选位置
公共职位(国际职位){
selectedPosition=位置;
}
}
现在在活动课上:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // set the custom adapter method and set the selectedposition with the listview position
        ((Category_Adapter) adapter).setSelected(position);


    }
@覆盖
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
//设置自定义适配器方法,并使用listview位置设置selectedposition
((U类适配器)适配器)。选择设置(位置);
}

试试这个,对我来说效果不错。

您使用的自定义适配器意味着自定义布局?对listview使用SimpleCorsOrAdapter和customlayout。:thislist.setBackgroundResource(R.drawable.OutlineView\u customshape);实际上,在自定义布局中,您必须将背景设置为选定行,并且它将保持选定状态。我这样尝试,但没有帮助:实际上,我可以为选定的单个项目设置背景色。接下来,如果我转到edittext,键盘将弹出,当我开始输入数据时,列表框中选定项目的背景色将消失对于这一点,当edittext被聚焦时,您必须将逻辑放入其中,并再次将背景设置为所选位置或从自定义适配器调用该函数。
@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // set the custom adapter method and set the selectedposition with the listview position
        ((Category_Adapter) adapter).setSelected(position);


    }