Reputation: 459
I want to make ListView with images. Something like this. But some of items have text only and don't have images. For those items I want to show text only. How can I do that?
Upvotes: 0
Views: 579
Reputation: 2135
Each item in a list view is a separate view, which can be created as needed. Simply inflate a view from two separate layouts in GetView method of your adapter class.
Something like this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return new MyViewItem(getImage(), getText(), parent);
}
*****
class MyViewItem extends LinearLayout
{
public MyViewItem(ImageClass image, String text, ViewGroup parent) {
super(parent);
View.inflate(parent, image==null?R.layout.layout1:R.layout.layout2, this);
//Now assign correct text and image using this.findViewById() function.
}
}
I leave it to you to define layout1 and layout2 as well as how to handle actual image and text, but this should do the trick.
Upvotes: 1
Reputation: 14321
You could also have 1 view, but just set visiblity to GONE on the image when you dont want the image to be there. As long as you've set up your layout appropriately, the text should pop over to the edge again just fine!
Upvotes: 0