[英]How to show the selected item in ListView (Android)
The problem is when the ListView
loses focus, the selected item isn't highlighted.问题是当
ListView
失去焦点时,所选项目未突出显示。
I use a custom ArrayAdapter
which uses the following layout for items:我使用自定义
ArrayAdapter
,它使用以下项目布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ListTextBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/Padding"
android:textSize="@dimen/TextSize"
/>
The ListSelector
is a simple Shape drawable. ListSelector
是一个简单的 Shape 可绘制对象。
Thank you谢谢
edit...编辑...
Here is the ArrayAdapter
:这是
ArrayAdapter
:
listWords = new ArrayList<String>();
arrayAdapter = new DictListAdapter(this, R.layout.listtext, listWords);
list.setAdapter(arrayAdapter);
and the code for DictListAdapter:和 DictListAdapter 的代码:
public class DictListAdapter extends ArrayAdapter<String> {
public DictListAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
this.context = (MainWindow) context;
this.objects = objects;
this.res = textViewResourceId;
}
Typeface font;
private List<String> objects;
private final MainWindow context;
int res;
int gravity=Gravity.LEFT;
@Override
public int getCount() {
return objects.size();
}
@Override
public String getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String obj = objects.get(position);
TextView tv = new TextView(context, null, res);
tv.setText(obj);
tv.setTypeface(context.getFont());
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,
context.getResources().getDimension(R.dimen.TextSize));
tv.setGravity(gravity);
return tv;
}
public void setGravity(int g){
this.gravity=g;
}
}
i guess you have created array adapter in the following manner.我猜你已经通过以下方式创建了数组适配器。 if i am correct use the following:
如果我是正确的使用以下:
static final String[] color = new String[]{"red","green","blue"};
We need to use the setListAdapter(..) method of the ListActivity class to bid the two (data and view).我们需要使用 ListActivity class 的 setListAdapter(..) 方法对两者(数据和视图)进行出价。 Android provides many implementations of the ListAdapter.
Android 提供了许多 ListAdapter 的实现。 We will use the simple ArrayAdapter.
我们将使用简单的 ArrayAdapter。
What does the ArrayAdapter expect? ArrayAdapter 期望什么?
It expects the context object, the row layout and the data in an array.它需要上下文 object、行布局和数组中的数据。
So here it is:所以这里是:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS));
getListView().setTextFilterEnabled(true);
}
use below code to show selected item使用以下代码显示所选项目
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the color: " + " " + color, Toast.LENGTH_LONG).show();
}
The simplest way is to add android:background="?android:attr/activatedBackgroundIndicator"
in the layout you use for the row最简单的方法是在用于行的布局中添加
android:background="?android:attr/activatedBackgroundIndicator"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.