My getView() Position is repeating In Custom List View? - android

I am using custom ListView .Its working fine. I am using this ListView for differentiate Read & Unread Messages I pick up message read Id which is 0 for unread message & 1 for read message.
My getView() code is following:---
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.custom_home_list, null);
holder=new ViewHolder();
holder.address=(TextView)convertView.findViewById(R.id.person_name);
holder.body=(TextView)convertView.findViewById(R.id.full_msg);
holder.date=(TextView)convertView.findViewById(R.id.msg_time);
convertView.setTag(holder);
}else
{
holder=(ViewHolder)convertView.getTag();
}
int size=mArrList.size();
if ((mArrList != null) || size > 0)
{
if(mArrList.get(position).read.equalsIgnoreCase("1")){
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.date.setText(mArrList.get(position).date);
}else{
holder.address.setText(mArrList.get(position).address);
holder.body.setText(mArrList.get(position).body);
holder.body.setTextColor(mArrList.get(position).color);
holder.date.setText(mArrList.get(position).date);
}
}
return convertView;
}
here i use this condition for differentiate :-
if(mArrList.get(position).read.equalsIgnoreCase("1")){
}
But Position Repeat After 5 items so my condition not working.
I have lots of search for this But I am not getting it.
Please Help me .
Thanks In Advance!
Regards Deepanker

In your code you don't explicitely set the color in case of a 'read' state (the first section of your if/else).
It means that if your view is recycled and it was previously used for an 'unread' item, then the color will remain the same.
You need to explicitely say which color you want in both cases, because you cannot know what the initial color is.

Related

How to add dynamically item in ListView with conditional cell type?

I try to add dynamic number of cell inside listview using custom cell layout and ArrayAdapter and it done well. But i face a problem when i want each cell may different follow type of data.
Example i have 3 categories inside a listview :
Video (video_custom_cell.xml)
Photo (photo_custom_cell.xml)
Audio (audio_custom_cell.xml)
if data is video then i use cell number one, else if data is photo i use photo_custom_cell and else i use audio_custom_cell.
What i have tried only can reuse a custom cell for dynamic number of row, but i still not find how to use custom cell follow by type of data inside cell.
Can anyone help me to explain an example for my problem?
Thank you.
You can use a custom adapter for this and inside the getView function you do for example:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if (convertView == null) {
if(items.get(position).type.equals("video")) {
rowView = getActivity().getLayoutInflater().inflate(
R.layout.video_custom_cell, null);
}else if(items.get(position).type.equals("photo")) {
rowView = getActivity().getLayoutInflater().inflate(
R.layout.photo_custom_cell, null);
}
} else {
rowView = convertView;
}
}

Erratic behaviour of listview (Android)

In my listview I have a custom Adapter, which I build using a TreeMap, I defined the getView method which is as follows. I am trying to strike out the text in a certian textview of the listview on click, only those textviews will be striken off which contain a certain pattern of characters (y#y). However on clicking one row in the listview I am getting strike out effect on some other row.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.chklistlayout, parent, false);
}
TextView textView = ((TextView) convertView.findViewById(R.id.textView1));
TextView imageview = ((TextView) convertView.findViewById(R.id.textView2));
textView.setText(values[position]);
imageview.setText(mValues[position]);
String s = mValues[position];
if (s.contains("y#y")) {
System.out.println("In if of getview");
System.out.println(s);
imageview.setPaintFlags(imageview.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
}
return convertView;
}
}
I tried using a holder pattern too, using a static holder class, but the problem seems to persist. Any pointers?
this answer is half mine and half from Muhammad Babar that we both commented on the question making together a quite nice answer:
use else block to handle this, imageview.setPaintFlags() not to strike
that happens
Because of the convertView. When you scroll the list the same view
that was used before is give back to getView method. So the same
imageView that you painted with StrikeThrough before is still painted
with it. So you have to revert that action.
more over, because of this recycling, usually dealing with Adapters you must ALWAYS undo/revert/repaint/change text/change image to all the elements to make sure it will be on screen the way you want.
ps.: now you should apply a Holder pattern to get better performance.

processing in ArrayAdapter

i am trying to show the contents using a list activity in android.
I am getting a list from backend. now i want to fetch each list value and see whether that item is shown in red color or in green color. this value will also be set in customVO.
I tried refering to few article on internet like this. but here they are using List only but i need VO so that i can fetch the value to decide whether that menu item should be red or green.
P.S i am beginner in android so excuse me if my question seems to be bit stupid.
thanks in advance :)
Just override the getView method to set the background:
final ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(
getActivity(),
R.layout.list_item,
myArray) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
/* Set values of TextViews here */
MyClass currentItem = getItem(position);
if (currentItem.getColor() == MyVoClass.GREEN) {
convertView.setBackgroundColor(0x0000FF00);
} else if (currentItem.getColor() == MyVoClass.RED) {
convertView.setBackgroundColor(0x00FF0000);
} else {
convertView.setBackgroundColor(0x00FFFFFF);
}
return convertView;
}
};

Difficulty in changing the background color of a particular row in an Android Custom ListView

I am confused in changing the background colour of a particular row in an list view, below is the code I tried. Different rows gets highlighted as i scroll the list, I could like to understand the reason behind this. The logic seems to quite simple, but the results are unpredictable. How am I supposed to achieve this.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rows_for_layout, null);
holder = new ViewHolder();
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.rated=(ImageView)convertView.findViewById(R.id.rated);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//selected_position is the position where the list has to be highlighted
if(position==selected_position){
holder.name.setText(elements.get(position).get("name"));
convertView.setBackgroundResource(R.drawable.highlight_this);
holder.rated.setBackgroundResource(R.drawable.star_image);
}else{
holder.name.setText(elements.get(position).get("name"));
}
return convertView;
}//getView ![alt text][1]
Your else statement does not reset the background color to its original. The getView method can recycle a view that was previously in your list but is not visible anymore. If the background was changed, then it will still be that background color from when it was originally created, which can depend on your state.
So, to "reset" that, add the following in your else:
if(position==selected_position){
holder.name.setText(elements.get(position).get("name"));
convertView.setBackgroundResource(R.drawable.highlight_this);
holder.rated.setBackgroundResource(R.drawable.star_image);
}else{
holder.name.setText(elements.get(position).get("name"));
//Add this
convertView.setBackgroundResource(R.drawable.not_highlighted);
}

ListView in ArrayAdapter order get's mixed up when scrolling

I have a ListView in a custom ArrayAdapter that displays an icon ImageView and a TextView in each row. When I make the list long enough to let you scroll through it, the order starts out right, but when I start to scroll down, some of the earlier entries start re-appearing. If I scroll back up, the old order changes. Doing this repeatedly eventually causes the entire list order to be seemingly random. So scrolling the list is either causing the child order to change, or the drawing is not refreshing correctly.
What could cause something like this to happen? I need the order the items are displayed to the user to be the same order they are added to the ArrayList, or at LEAST to remain in one static order. If I need to provide more detailed information, please let me know. Any help is appreciated. Thanks.
I was having similar issues, but when clicking an item in the custom list, the items on the screen would reverse in sequence. If I clicked again, they'd reverse back to where they were originally.
After reading this, I checked my code where I overload the getView method. I was getting the view from the convertedView, and if it was null, that's when I'd build my stuff. However, after placing a breakpoint, I found that it was calling this method on every click and on subsequent clicks, the convertedView was not null therefore the items weren't being set.
Here is an example of what it was:
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listitemrow, null);
RssItem rssItem = (RssItem) super.getItem(position);
if (rssItem != null)
{
TextView title = (TextView) view.findViewById(R.id.rowtitle);
if (title != null)
{
title.setText(rssItem.getTitle());
}
}
}
return view;
}
The subtle change is moving the close brace for the null check on the view to just after inflating:
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listitemrow, null);
}
RssItem rssItem = (RssItem) super.getItem(position);
if (rssItem != null)
{
TextView title = (TextView) view.findViewById(R.id.rowtitle);
if (title != null)
{
title.setText(rssItem.getTitle());
}
}
return view;
}
I hope this helps others who experience this same problem.
To further clarify the answer of farcats below in more general way, here is my explanation:
The vi.inflate operation (needed here for parsing of the layout of a row from XML and creating the appropriate View object) is wrapped by an if (view == null) statement for efficiency, so the inflation of the same object will not happen again and again every time it pops into view.
HOWEVER, the other parts of the getView method are used to set other parameters and therefore should NOT be included within the if (view == null) statement.
Similarily, in other common implementation of this method, some textView, ImageView or ImageButton elements need to be populated by values from the list[position], using findViewById and after that .setText or .setImageBitmap operations.
These operations must come after both creating a view from scratch by inflation and getting an existing view if not null.
Another good example where this solution is applied for BaseAdapter appears in BaseAdapter causing ListView to go out of order when scrolled
The ListView reuses view objects when you scroll. Are you overriding the getView method? You need to make sure you set each property for every view, don't assume that it will remember what you had before. If you post that method, someone can probably point you at the part that is incorrect.
I have a ListView, AdapterView and a View (search_options) that contains EditText and 3 Spinners. ListView items are multiple copies of (search_options) layout, where user can add more options in ListView then click search to send sql query built according to users options.
I found that convertView mixing indecies so I added a global list (myViews) in activity and passed it to ArrayAdapter. Then in ArrayAdapter (getView) I add every newly added view to it (myViews).
Also on getView instead of checking if convertView is null, I check if the global list (myViews) has a view on the selected (position).. It totally solved problems after losing 3 days reading the internet!!
1- on Activity add this:
Map<Integer, View> myViews = new HashMap<>();
and then pass it to ArrayAdapter using adapter constructor.
mSOAdapter = new SearchOptionsAdapter(getActivity(), resultStrs, myViews);
2- on getView:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder viewHolder;
if (!myViews.containsKey(position)) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.search_options, parent, false);
/// ...... YOUR CODE
myViews.put(position, view);
FontUtils.setCustomFontsIn(view, getContext().getAssets());
}else {
view = myViews.get(position);
}
return view;
}
Finally no more mixing items...

Categories

Resources