I have customised list view using BaseAdapter, I want to give separate color for the row position%5 ==0. but it also change the color some other rows which not satisfy the condition. Following is my getView().
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=convertView;
if(convertView==null){
view=inflater.inflate(R.layout.groupl_ist, null);
}
TextView countryName=(TextView) view.findViewById(R.id.tvCountryText);
ImageView countryImage=(ImageView) view.findViewById(R.id.imgCountry);
String label=countryNames.get(position);
countryName.setText(label);
if(position%5==0){
countryImage.setVisibility(LinearLayout.GONE);
view.setBackgroundColor(Color.GREEN);
}else{
countryImage.setImageResource(R.drawable.ic_launcher);
}
return view;
}
It looks like you're having an issue with recycling. You need to implement the ViewHolder pattern. See this link for details on how to implement.
Ultimately what you'll be doing is holding onto the view as you scroll up and down so that it redraws correctly.
I am not pretty sure why is happened your problem but try to set to the original color to the rows that don't satisfy the condition. Also, you should implement the ViewHolder pattern. There are a lots post about this pattern.
if(position%5==0){
view.setBackgroundColor(Color.GREEN);
}else{
view.setBackgroundColor(Color.WHITE); //whatever color
}
Related
I have used this tutorial in order to create a custom listview, but for some reason it is behaving differently than what I expect.
Here is the code I changed and am currently using for the adapter:
public View getView(int position, View convertView, ViewGroup parent) {
// same code as in example with some differences in the .xml
viewHolder.itemLabel.setText(info.split(";")[0]);
viewHolder.itemDescription.setText(info.split(";")[1]);
viewHolder.itemLabel.setOnClickListener(this);
viewHolder.itemLabel.setTag(position);
if(viewHolder.itemLabel.getText().equals("Fat")){
System.out.println(info);
System.out.println(viewHolder.itemLabel.getText());
viewHolder.itemLabel.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.info_icon, 0);
}
return convertView;
}
Now, what is happening is that for some reason the icon is being set for more than one item on the list even though there is only one with "Fat" on the label.
The print is actually showing that the code on the if is being run three times.
I think that this must have something to do with the lifecycle of the app, but i'm not sure about it.
I'm sorry if the question is to vague, but I couldn't find a better way to ask this.
You need to add the else part inside getView().
public View getView(int position, View convertView, ViewGroup parent) {
// same code as in example with some differences in the .xml
if(viewHolder.itemLabel.getText().equals("Fat")){
// Stuff
}else{
// Else stuff
}
return convertView;
}
ConvertView parameter is a recycled instance of View that you previously returned from getView(). You can read about it just search for it .
Making some quick points . You are not using viewholder pattern correctly in code. Use it properly look for ViewHolder pattern in List view. Apart from that you should Move to RecyclerView ListView is in legacy now.
Please check code It may be helpful.
public View getView(int position, View convertView, ViewGroup parent) {
// same code as in example with some differences in the .xml
viewHolder.itemLabel.setText(info.split(";")[0]);
viewHolder.itemDescription.setText(info.split(";")[1]);
viewHolder.itemLabel.setOnClickListener(this);
viewHolder.itemLabel.setTag(position);
if(viewHolder.itemLabel.getText().equals("Fat")){
System.out.println(info);
System.out.println(viewHolder.itemLabel.getText());
viewHolder.itemLabel.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.info_icon, 0);
}else{
viewHolder.itemLabel.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
return convertView;
}
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.
I have a row in a listview that I need to change its layout dynamically. In my array adapter I have the following (simplified) code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.default);
}
...
if(condition) {
View view = inflater.inflate(R.layout.new);
...
return view; // doesn't work
}
return convertView;
}
My actual listview is actually pretty complex and I am also using the getViewType for the two different layouts. But the above code should be enough to make my point that the new layout won't display correctly when I want to switch layout dynamically.
My question is, how can I refresh the convertView, or trigger a reload of the adapter/listview to the point that the convertViews will be cleared? I've tried calling notifyDataSetChanged, or calling the listview invalidateViews() but they all didn't worked. My last resort seems to restart the whole activity but I don't think this is an elegant solution.
I am working on customize (inflated) list view. In which I have used the text and background image for text (as per the condition).
Now I am facing problem in scrolling the list view that background of text view is overlapping to the other text views.
Here is the sample code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View icontextlayout=convertView;
icontextlayout= inflater.inflate(R.layout.layout_complex_list, null);
TextView Txt1=(TextView)icontextlayout.findViewById(R.id.txt1);
if(disp1==true)
{
Txt1.setBackgroundResource(R.drawable.pic)
}
else
Txt1.setText("Text1 "+strUser);//
TextView Txt2=(TextView)icontextlayout.findViewById(R.id.txt2);
if(disp2==true)
{
Txt2.setBackgroundResource(R.drawable.pic);
}
else Txt2.setText("Text2: "+strIndus);
return icontextlayout;
}
Could you please help me out that background image pic do not overlap the others background.
Thanking you...
The problem is that you must set a default background when you don't need a background. For instance:
if(disp1==true){
Txt1.setBackgroundResource(R.drawable.pic);
Txt1.setText("");
}
else{
Txt1.setText("Text1 "+strUser);//
Txt1.setBackgroundDrawable(null);
}
Also, if you don't mind, I like to give you my opinion about your code:
That's not they way in which list are usually populated. Take a look at this answer: How to load the Listview "smoothly" in android
convertView is used to reuse rows. In your case you are doing something like:
View icontextlayout=convertView;
icontextlayout= inflater.inflate(R.layout.layout_complex_list, null);
Which is bad, because you are not actually using the convertView (when you call inflater.inflate) it will create a new row, thus your list will be really slow.
if(disp2==true) is redundant. You should consider using just: if(disp2).
I don't know if it is possible, but actually I wouldn't see why not.
Can we do a grid view not just with ImageView but with a custom view.
I am trying to make a grid view of a view composed of an ImageView and a TextView.
I know that everything happens in my Adapter getView's function but I can't figure out how to do it.
public View getView(int position, View convertView, ViewGroup parent) {
View cases = findViewById(R.id.fileUnitLayout);
if (convertView == null) {
convertView = new View(mContext);
} else {
cases = convertView;
}
return cases;
}
My view has an id of R.id.fileUnitLayout. Let's say my inner TextView has an id of A and my inner ImageView has an id of B. How can I fill them ?
Thank you,
You should not need to override getView to accomplish this, necessarily. GridView is an AdapterView so you can provide an adapter that will display what you want via setAdapter
You could, for example, use SimpleAdapter to provide an xml file that is used for each grid view item.