开发者

Trouble in inflated listview

开发者 https://www.devze.com 2023-02-08 19:50 出处:网络
I am working on customize (inflated) l开发者_开发技巧ist view. In which I have used the text and background image for text (as per the condition).

I am working on customize (inflated) l开发者_开发技巧ist 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).
0

精彩评论

暂无评论...
验证码 换一张
取 消