Trouble in inflated listview - android

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).

Related

Dynamically changing the background of ListView child

I read many other posts, but most, if not all of them tell you to override one of the methods and put change the background then. I want to change it outside of the ListView Adapter class, so I tried doing this. It doesn't seem to work. I do however get the object, nut it wont change its background.
RelativeLayout item;
item = (RelativeLayout) listView.getAdapter().getView(0, null, listView);
item.setBackgroundColor(Color.YELLOW);
Thank you.
Find the nearest time save it in a variable and call adapter.notifyDataSetChanged()
public View getView(int position, View convertView, ViewGroup parent){
if(time is nerest time) {
convertView. setBackgroundColor(Color. YELLOW)
} else {
convertView. setBackgroundColor("default color")
}
}

How to set background color for all elements of ListView on OnItemClick event?

I have a ListView and posibility to select one element (single choice).
How can I set background color for all elements of ListView (maybe which are visible at least) when some item was selected?
adapter = new ArrayAdapter<Orderline>(activity, simple_list_item_single_choice, orderlines) {
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
...
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
convertView.setBackgroundColor(BLACK);
// so here currently selected element is set to BLACK, but also other elements have to be set to WHITE
}
});
return convertView;
}
}
I don't have much code for you because I'm not at my workstation right now, but I'm thinking you can just set the background of the selected item to black by via your onItemClick as you've already suggested. Cool.
To change the color of the other(unselected) views when a particular view is selected, I'm guessing you can call your Adapter's getCount() and loop through that list, make a call to getChildAt(i) of your ListView. This returns a View which you can call setBackgroundColor(Color) on. Hope this helps
You can do
parent.setBackgroundColor(BLACK);
this.notifyDataSetChanged();
to set your list view background.
I would use a Drawable selector.
Check this link for a pretty good example:
http://www.charlesharley.com/2012/programming/custom-drawable-states-in-android/
Basically you create the XML drawable which contains a mapping (which the system uses automatically) to what you would like displayed when certain click events occur.
You can assign the background drawable to any view, so you could do it in your XML for your adapter, or in Java code. Which in your case might be something like this:
convertView.setBackgroundDrawable(R.drawable.MySelector);
parent.setBackgroundColor(while_color);
v.setBackgroundColor(black_color)

Change the text on changing image in android

I have a horizontal scroll view with array list of images and the text field below the scroll view,how could i change the text on changing image in horizontal scrollview.
For Example: The image will show Mountain the text will also texted with mountain
Here The image will only scroll, the text view will remain fix, it will change the name with images only
I have used the code below for scrolling image ,it is working fine, images are changing but the textview didn't change !!
could anybody help me !! #Thanks in advance
public View getView(int position, View convertView, ViewGroup parent) {
vi=convertView;
if(convertView==null){
vi = LayoutInflater.from(parent.getContext()).inflate(R.layout.screen, null);
position_pin = position;
Holder.images=(ImageView) vi.findViewById(R.id.image);
Holder.textview=(TextView) vi.findViewById(R.id.name);
Holder.images.setImageResource(imageIds[position]);
Holder.text.setText(name);
vi.setTag(Holder);
}
return vi;
}
If you have any better option or code then help me.
I guess problem is in about your way to get correspond name.But you have an option(however it is strange!):
You can merge each image and it's description to another image and only show the result.If you want to do this,you can use your R.layout.screen or directly use a canvas and add your image and desired description to it.
Your code suggests that you set the text only when the convertView is null. Generally adapter views are re-cycled so the update should happen even when a new view isn't formed.
Arraylist<String> nameList = new ArrayList<String>();
// add your names the same way you add your images to your images array
...
name = nameList.get(position);
textView.setText(name);
I would think something like that should work
It looks like you are using a ViewHolder so you should add to your if statement
else
{
Holder = (ViewHolder) vi.getTag();
}
To reuse your Views

android adapter getView does not repaint image on scroll and notification event

Hi I have a list and I am redrawing the image based on the setting on notification. Basically I can't figure out why the image is NOT redrawn. I see in the log that it gets set.
Odly I tried to set a textview in the same view and that works fine. What's wrong with the image? can anybody tell me?
Thank you in advance!
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
//---- if the view is null, create a new one from xml
if (convertView == null)
{
LayoutInflater layoutInflater = _context.getLayoutInflater();
convertView = layoutInflater.inflate(R.drawable.my_item_layout, parent, false);
convertView.setBackgroundResource(R.drawable.mybg);
}
CustomItem item = super.getItem(position);
if (item != null)
{
TextView title = (TextView)convertView.findViewById(R.id.titleTextView);
ImageView image1= (ImageView)convertView.findViewById(R.id.image1);
title.setText(item.getName());
if (item.getMyNotificationChangedItemState()){
image1.setImageResource(R.drawable.myimagePink); <- this does not get updated
title.setTextColor(this._context.getResources().getColor(R.color.main_text_pink));
else
{
image1.setImageResource(R.drawable.myimageBlue);
title.setTextColor(this._context.getResources().getColor(R.color.main_text_blue));
}
}
So my Blue image & text gets shown when the list gets loaded the first time. Once notification happens I first update to change the one that's currrently PINK to BLUE and then change the one that I need to update from BLUE to PINK depending on previous and current index of the items in the list. The update is done on the activity. I can see the log prints the fact that getMyNotificationChangedItemState() is changed, and the image is being set, but I never see the change in the UI.
Why does that work for TEXTVIEW and does not work for IMAGEVIEW?
For adapters, getView() only gets called when the current view is invalidated.
Have you made sure to call invalidate() on the view in question after you make the notification?
As another sanity check, what about switching pink and blue and trying again?

Background Color Recycled in ListView

I have a strange problem. I am setting the background color of the items of a listview like so:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
...
if (status == true) {
row.setBackgroundColor(Color.argb(255,0,85,187));
}
else {
if (morestuff) {
row.setBackgroundColor(Color.argb(128,255,0,0));
}
}
...
}
This seems to work. However, when i scroll on the listview, and then back, some of the rows have acquired a color from another row without being set by this code. I suspect the listview is recycling the views as an optimization.
How can I fix this?
The getView will be called all the time when the listview is drawn. Simply taking it will be called when we do a small change example do a small scroll
If you want to set color to a specific row, Just do it by checking the position (First argument of getview).
The list view is definately recycling views as an optimization. You should look at the efficient list view example for ideas.

Categories

Resources