I am trying to create a listview that receives a flagged list of items to indicate a status to the user. I have been able to create the flag display by using a yellow background (see image at bottom). In Theory, the flagged list can have many flagged items in it. However in my app, only the first three flagged backgrounds are shown. I believe this is because they are initially drawn to the screen. The Flagged background that are not drawn initially to the screen do not show. I'd like to know how to get the remaining flags to show in the list.
ListView Recycling:
The backgrounds in the listView are being recycled in getView(). This recycling goes from position 0 to position 9. I have flags that need to match at positions 13, 14 and so on. Those positions are not being displayed.
listView.getCheckedItemPositions() for multiple selections:
This method will not work in my case because the user will not selected the flags. The flags are coming from the server.
setNotifyOnChange() and/or public virtual void SetNotifyOnChange (bool notifyOnChange):
I'm not adding new data to the list, so I don't see how this method would work for my program. Does this method communicate to getview when it is recycling data? I was unable to find an answer to this in my research.
public void registerDataSetObserver:
This may be overkill for my problem, but is it possible to have an observer object that keeps track of the all the positions in my items list and in my flag list no matter if the view is recycled and match them on the screen?
Code:
package com.convention.notification.app;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NewsRowAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private List<Integer> disable;
View view ;
int disableView;
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList, List<Integer> disableList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
this.disable=disableList;
System.out.println("results of delete list a:"+disable.toString());
}
public int getCount() {
return items.size();
}
public Item getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
for(int k =0;k < disable.size();k++){
if(position==disable.get(k)){
//System.out.println( "is "+position+" value of disable "+disable.get(k));
disableView=disable.get(k);
//AdapterView.getItemAtPosition(position);
}
}
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
getItemViewType(position);
long id=getItemId(position);
if(position==disableView){
view.setBackgroundColor(Color.YELLOW);
System.out.println(" background set to yellow at position "+position +" disableView is at "+disableView);
}else{
view.setBackgroundColor(Color.WHITE);
System.out.println(" background set to white at position "+position +" disableView is at "+disableView);
}
//ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
holder = new ViewHolder();
/* setTag Sets the tag associated with this view. A tag can be used to
* mark a view in its hierarchy and does not have to be unique
* within the hierarchy. Tags can also be used to store data within
* a view without resorting to another data structure.
*/
view.setTag(holder);
} else {
//the Object stored in this view as a tag
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tv_event_name = (TextView) view.findViewById(R.id.tv_event_name);
holder.tv_event_date = (TextView) view.findViewById(R.id.tv_event_date);
holder.tv_event_start = (TextView) view.findViewById(R.id.tv_event_start);
holder.tv_event_end = (TextView) view.findViewById(R.id.tv_event_end);
holder.tv_event_location = (TextView) view.findViewById(R.id.tv_event_location);
if (holder.tv_event_name != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tv_event_name.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tv_event_date != null && null != objBean.getDate()
&& objBean.getDate().trim().length() > 0) {
holder.tv_event_date.setText(Html.fromHtml(objBean.getDate()));
}
if (holder.tv_event_start != null && null != objBean.getStartTime()
&& objBean.getStartTime().trim().length() > 0) {
holder.tv_event_start.setText(Html.fromHtml(objBean.getStartTime()));
}
if (holder.tv_event_end != null && null != objBean.getEndTime()
&& objBean.getEndTime().trim().length() > 0) {
holder.tv_event_end.setText(Html.fromHtml(objBean.getEndTime()));
}
if (holder.tv_event_location != null && null != objBean.getLocation ()
&& objBean.getLocation ().trim().length() > 0) {
holder.tv_event_location.setText(Html.fromHtml(objBean.getLocation ()));
}
return view;
}
public class ViewHolder {
public TextView
tv_event_name,
tv_event_date,
tv_event_start,
tv_event_end,
tv_event_location
/*tv_event_delete_flag*/;
}
}
Logcat:
06-12 20:54:12.058: I/System.out(493): item disalbed is at postion :0
06-12 20:54:12.058: I/System.out(493): item disalbed is at postion :4
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :5
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :13
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :14
06-12 20:54:12.069: I/System.out(493): item disalbed is at postion :17
06-12 20:54:12.069: I/System.out(493): results of delete list :[0, 4, 5, 13, 14, 17]
06-12 20:54:12.069: I/System.out(493): results of delete list a:[0, 4, 5, 13, 14, 17]
06-12 20:54:12.069: I/System.out(493): set adapaer to list view called;
06-12 20:54:12.128: I/System.out(493): background set to yellow at position 0 disableView is at 0
06-12 20:54:12.628: I/System.out(493): background set to white at position 1 disableView is at 0
06-12 20:54:12.678: I/System.out(493): background set to white at position 2 disableView is at 0
06-12 20:54:12.708: I/System.out(493): background set to white at position 3 disableView is at 0
06-12 20:54:12.738: I/System.out(493): background set to yellow at position 4 disableView is at 4
06-12 20:54:12.778: I/System.out(493): background set to yellow at position 5 disableView is at 5
06-12 20:54:12.808: I/System.out(493): background set to white at position 6 disableView is at 5
06-12 20:54:12.838: I/System.out(493): background set to white at position 7 disableView is at 5
This is a link to my first question a day ago:
Change Background on a specific row based on a condition in Custom Adapter
I appreciate your help!