I have a MapActivity and ListActivity in one screen, when the user clicks one of the Pin on the Map, the clicked location will be on the top of the List and have a different background and divider color.
So I initiate to send the latitude of the clicked Pin and retrieve it on the getView(), if the clicked latitude is same with the displayed latitude on the first entry, it will do something.
I’ve managed to make the first entry of the ListView to have a certain background, but when I scroll down the List, some of the other entries’ background are also changed.
Here is my method:
double selectedLat = WWHApplication.getSelectedLatitude();
DecimalFormat df = new DecimalFormat("#.#####");
String dLat = df.format(lat);
String sLat = df.format(selectedLat);
if (position == 0) {
if (dLat.equals(sLat)) {
feedViewHolder.layout
.setBackgroundResource(R.drawable.list_segment_selected);
}
}
How to change the color of the divider and the background of the ListView only on the first item?
Updated
Here’s the getView() method
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FeedViewHolder feedViewHolder = null;
final VideoLocationDB vidLocation = videoLocationsDB[position];
String url = vidLocation.documentary_thumbnail_url;
String name = vidLocation.name;
String title = vidLocation.name;
String desc = vidLocation.text;
double lat = vidLocation.latitude;
double lng = vidLocation.longitude;
String distance = calculateDistance(lat, lng);
// System.out.println("ON LOC DISTANCE: "+distance);
if (convertView == null) {
// convertView = LocationsListActivity.this.getLayoutInflater()
// .inflate(R.layout.listitems, null, true);
convertView = layoutInflater.inflate(R.layout.listitems,
parent, false);
feedViewHolder = new FeedViewHolder();
feedViewHolder.layout = (LinearLayout) convertView
.findViewById(R.id.list_bg);
feedViewHolder.titleView = (TextView) convertView
.findViewById(R.id.txt_title);
feedViewHolder.descView = (TextView) convertView
.findViewById(R.id.txt_list_desc);
feedViewHolder.more = (TextView) convertView
.findViewById(R.id.txt_more);
feedViewHolder.distanceView = (TextView) convertView
.findViewById(R.id.txt_distance);
feedViewHolder.v = (ImageView) convertView
.findViewById(R.id.image);
Typeface fontRegular = Typeface.createFromAsset(getAssets(),
"miso.otf");
feedViewHolder.titleView.setTypeface(fontRegular);
Typeface fontLight = Typeface.createFromAsset(getAssets(),
"miso-light.otf");
feedViewHolder.descView.setTypeface(fontLight);
feedViewHolder.more.setTypeface(fontLight);
feedViewHolder.distanceView.setTypeface(fontRegular);
convertView.setTag(feedViewHolder);
} else {
feedViewHolder = (FeedViewHolder) convertView.getTag();
}
feedViewHolder.v.setTag(url);
loader.DisplayImage(url, LocationsListActivity.this,
feedViewHolder.v, name);
double selectedLat = WWHApplication.getSelectedLatitude();
DecimalFormat df = new DecimalFormat("#.#####");
String dLat = df.format(lat);
String sLat = df.format(selectedLat);
if (position == 0) {
if (dLat.equals(sLat)) {
feedViewHolder.layout
.setBackgroundResource(R.drawable.list_segment_selected);
}
}
feedViewHolder.titleView.setText(title.toUpperCase());
feedViewHolder.descView.setText(desc);
feedViewHolder.more.setText(getString(R.string.de_list_more));
feedViewHolder.distanceView.setText(distance);
return convertView;
}
I don’t think the stock ListView supports that behavior really.
You could fake it by setting the dividers on the ListView to 0, and adding in the dividers yourself as “plain” rows. Since you can make a row look like whatever you want, presumably you could inflate a view that looks like whatever kind of divider you want and add it to the list in-between each item. It should be possible to implement that logic in your adapters getView() method.
EDIT:
I think it should fix if you add an else block to your if that is changing the color like this:
and replace ‘list_normal’ with whatever your default background drawable is. If you didn’t set one probably what you need is something like
android.R.drawable.list_selector_backgroundYou may have to look in the sdk res drawable folder to find the correct name, I could be wrong on its exact title. I checked, and I think that is the one you need. But I didn’t test it on device.