Reputation: 45991
I'm developing an Android 2.3.3 application with a ListActivity.
I want to do something to show to the user that what was item selected by he/she. I have done the following but it doesn't (and I was searching in stackoverflow and they are contradictory answers).
This is layout_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector"
android:text="" />
</LinearLayout>
This is the selector.xml file on res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#00FF00" />
<item android:state_pressed="true"
android:color="#555555" />
<item android:color="#000000" />
</selector>
But it doesn't work because it says that I have to put a @drawable
in selector.xml
.
How can I set background color to blue to last list item clicked?
UPDATE
Array adapter used with this list:
public class GatesAdapter extends ArrayAdapter<Gate>
{
/**
* Application context.
*/
private Context context;
/**
*
*/
private int itemLayoutId;
/**
*
*/
private ArrayList<Gate> gates;
private int selectedGateIndex;
public int getSelectedGateIndex() {
return selectedGateIndex;
}
public void setSelectedGateIndex(int selectedGateIndex) {
this.selectedGateIndex = selectedGateIndex;
}
public Gate getSelectedGate()
{
return gates.get(selectedGateIndex);
}
public void removeSelectedGate()
{
this.gates.remove(selectedGateIndex);
}
public ArrayList<Gate> getGates()
{
return this.gates;
}
public GatesAdapter(Context context, int listItemResourceId,
ArrayList<Gate> objects)
{
super(context, listItemResourceId, objects);
this.context = context;
this.itemLayoutId = listItemResourceId;
this.gates = objects;
this.selectedGateIndex = -1;
this.setNotifyOnChange(true);
}
@Override
public int getCount()
{
return gates.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
Log.v("GatesAdapter", "getView.postion: " + position);
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(itemLayoutId, parent, false);
}
Gate gate = gates.get(position);
if (gate != null)
{
TextView itemText = (TextView)row.findViewById(android.R.id.text1);
if (itemText != null)
{
itemText.setText(gate.getName());
//selectedGateIndex = position;
if (selectedGateIndex == position)
{
row.setBackgroundColor(Color.BLUE);
}
}
}
return row;
}
}
Upvotes: 1
Views: 4853
Reputation: 1
I rather prefer this simpler solution:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
statusText.setText("Redes:" + lv.getCount()+"\n Posición / First:" +position + "/" + lv.getFirstVisiblePosition());
NetsArrayAdapter aA = (NetsArrayAdapter) parent.getAdapter();
for(int a = 0; a < parent.getChildCount(); a++) {
parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(Color.BLUE);
aA.setLastSelected(position);
}
});
private class NetsArrayAdapter extends ArrayAdapter<String> {
private ArrayList<String> mData;
private int id;
private Context c;
private int lastSelected = -1;
// declaring our ArrayList of items
private ArrayList<String> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<Item> objects,
* because it is the list of objects we want to display.
*/
public NetsArrayAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
mData = objects;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
View currView = super.getView(position, view, parent);
System.out.println("Paint / selected:"+position+" / "+lastSelected);
if(lastSelected != position)
currView.setBackgroundColor(Color.BLACK);
else
currView.setBackgroundColor(Color.BLUE);
return currView;
}
public int getLastSelected() {
return lastSelected;
}
public void setLastSelected(int lastSelected) {
this.lastSelected = lastSelected;
}
}
Upvotes: 0
Reputation: 5246
I've done it with custom array adapter, just checked there in getView()
method:
if (position == lastClicked) {
v.setBackgroundColor(0x...)
} else {
v.setBackgroundColor(0x...)
}
the else block is important due to a specific behavior of large listviews
UPDATE: here's my custom array adapter which works fine:
public class AbcArrayAdapter extends ArrayAdapter<UniversalListItem> {
private Context c;
private int id;
private List<UniversalListItem>items;
public AbcArrayAdapter(Context context, int viewResourceId, List<UniversalListItem> objects){
super(context,viewResourceId,objects);
c=context;
id=viewResourceId;
items=objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
final UniversalListItem o = items.get(position);
if (o != null) {
if ( o.isInList() ) {
v.setBackgroundColor(0x00ffffff);
} else {
v.setBackgroundColor(0x4d0099cc);
}
/*....*/
}
return v;
}
}
Upvotes: 1
Reputation: 4354
Yes, items in selectors only accept drawables. You can however create drawables with a color: add your colors in your colors.xml:
<drawable name="color1">#00FF00</drawable>
<drawable name="color2">#00FF00</drawable>
<drawable name="color3">#000000</drawable>
Then, use them as drawables:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/color1" />
<item android:state_pressed="true"
android:drawable="@drawable/color2" />
<item android:drawable="@drawable/color3" />
</selector>
Hope this will help you =)
Upvotes: 0