我怎样才能获得的ListView与CursorAdapter的行强调,当触摸就可以了? [英] How can I get row of ListView with CursorAdapter highlighted when touch on it?
问题描述
通常情况下,当我创建简单的ArrayAdapter一个的ListView
。它会自动得到这个亮点功能,无需额外的设置。然而,当我创建这个的ListView
与我的自定义的CursorAdapter
这个功能似乎丢失,我不能找到一种方法,解决这个问题。
Normally when I create a ListView
in simple ArrayAdapter. It got this highlight feature automatically without additional setup. However, when I create this ListView
with my custom CursorAdapter
this feature seem to be missing and I cannot find a way to solve it.
下面是我的自定义的CursorAdapter 公共类RecentCallAdapter扩展的CursorAdapter {
Here is my custom CursorAdapter public class RecentCallAdapter extends CursorAdapter {
private final String tag = this.getClass().getSimpleName();
public RecentCallAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
// TODO Auto-generated constructor stub
}
private static class ViewHolder {
TextView name;
TextView date;
int nameCol;
int dateCol;
int numberCol;
Calendar cal;
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
// TODO Auto-generated method stub
ViewHolder holder = (ViewHolder) v.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.recentcall_item_name);
holder.date = (TextView) v.findViewById(R.id.recentcall_item_date);
holder.nameCol = cursor.getColumnIndex(Calls.CACHED_NAME);
holder.dateCol = cursor.getColumnIndex(Calls.DATE);
holder.numberCol = cursor.getColumnIndex(Calls.NUMBER);
holder.cal = Calendar.getInstance();
v.setTag(holder);
}
String name = cursor.getString(holder.nameCol);
if(name == null){
name = cursor.getString(holder.numberCol);
}
holder.name.setText(name);
holder.cal.setTimeInMillis(Long.valueOf(cursor.getString(holder.dateCol)));
holder.date.setText(Utility.calculateTimePass(holder.cal.getTime()));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.recentcall_item, parent, false);
bindView(v, context, cursor);
return v;
}
什么办法解决呢? 谢谢。
Any way to solve this? Thank.
推荐答案
使用选择同一以下
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@android:drawable/list_selector_background">
</item>
<item android:state_focused="true" android:state_selected="true" android:drawable="@android:drawable/list_selector_background">
</item>
</selector>
和使用code。在列表视图标签设置选择
and use this code in listview tag for set selector
android:drawSelectorOnTop="true"
android:listSelector="@drawable/selector"
这篇关于我怎样才能获得的ListView与CursorAdapter的行强调,当触摸就可以了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!