Android listview array adapter selected
Posted
by
João Melo
on Stack Overflow
See other posts from Stack Overflow
or by João Melo
Published on 2012-04-11T11:26:12Z
Indexed on
2012/04/11
11:29 UTC
Read the original article
Hit count: 309
i'm trying to add a contextual action mode to a listview, but i'm having some problems with the selection, if i make aList1.setSelection(position)
it doesn't select anything, and if i make List1.setItemChecked(position, true)
it works but it only changes the font color a little and i want it to change the background or something more notable, is there any way to detect the selection and manually and change the background, or i'm missing something?
the list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="false">
</ListView>
</RelativeLayout>
the adapter:
public class ServicesRowAdapter extends ArrayAdapter<String[]> {
private final Activity context;
private final ArrayList<String[]> names;
static class ViewHolder {
public TextView Id;
public TextView Date;
public RelativeLayout statusbar,bglayout;
}
public ServicesRowAdapter(Activity context, ArrayList<String[]> names) {
super(context, R.layout.servicesrowlayout, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.servicesrowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Id = (TextView) rowView.findViewById(R.id.idlabel);
viewHolder.Date = (TextView) rowView.findViewById(R.id.datelabel);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.Date.setText(names.get(position)[2]);
holder.Id.setText(names.get(position)[1]);
return rowView;
}
}
with the use of a layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/idlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:text="@+id/idlabel"
android:textSize="20dp"
android:width="70dp" >
</TextView>
<TextView
android:id="@+id/datelabel"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/datelabel"
android:textSize="20dp"
android:layout_marginLeft="90dp" >
</TextView>
</RelativeLayout
© Stack Overflow or respective owner