Android setBackground or text color for muticolumn Listview 's particular row
How we set ListView's background for particular row.
ListView list = getListView();
sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode开发者_JS百科,R.id.routeName,R.id.outlets});
list.setAdapter(sd);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setSelected(true);
list.setTextFilterEnabled(true);
list.setItemsCanFocus(true);
list.setSelectionAfterHeaderView();
if(isVisitedToVisitHeader()){
if(defaultRoutePos != 0){
Log.i("-----" ,"---defaultRoutePos---" + defaultRoutePos +" --count-- "+list.getChildCount());
list.setItemChecked(defaultRoutePos, true);
// list.getChildAt(positions).setBackgroundColor(R.color.red);
//list.getChildAt(defaultRoutePos).setBackgroundColor(R.color.red);
}
}
this is my code .I have done using SimpleAdapter
.Here list.getChildCount()
give 0.This part I am ding in the onCreate()
.
Please help me...
Thanks in advance
Checkout this example of Custom ListView,
Example
Also this answer of StackOverflow
StackOverflow Answer
package com.android.listactivity;
import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
public MySimpleArrayAdapter(Activity context, String[] names) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.rowlayout, null, true);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(names[position]);
// Change the icon for Windows and iPhone
String s = names[position];
//Here you can do anythng with row of particular position
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
imageView.setImageResource(R.drawable.no);
} else {
imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}
I suggest to use Custom Adapter as shown above:
If not you can try to get list.getPositionForView(view) set background color or image for desired particular positions
精彩评论