Question

Stackflowers!

Little question.. I load text into a textView who is in a listView. I load the text out of an url. Everything works fine.. But.. Sometimes in the text there is an new url with an image. I can filter the text, and show the url only in a Log or something, no problem. But now.. I have many textView in my listView.. But now, when I detect a new image url, I want to create or show the image in an imageView. (To load the image is not a problem to..) But the problem is, how to hide all the imageViews.. Untill I (or my code..) detects an image url (.jpg)?

But sometimes there is one image.. on the 1st position of my listView.. Sometimes there are maybe 2 or 3 images.. On position 1, 5, 7.. I don't know that of beforehand..

This is my View Class:

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View item_view_second = convertView;
        post_second current_post = my_post_second.get(position);
        Typeface TF = Typeface.createFromAsset(getAssets(), global.fontType_title);
        Typeface tf = Typeface.createFromAsset(getAssets(), global.fontType_text);

        if (item_view_second == null) {
            item_view_second = getLayoutInflater().inflate(R.layout.item_view_second, parent, false);
        }

        // Title:
        TextView title_text = (TextView) item_view_second.findViewById(R.id.item_title);
        if(position == 0) {
        title_text.setVisibility(View.VISIBLE);
        title_text.setText(Html.fromHtml(current_post.get_title()));
        title_text.setTypeface(TF);
        } else {
            title_text.setVisibility(View.GONE);    
        }

        // Blog:
        TextView blog_text = (TextView) item_view_second.findViewById(R.id.item_blog);
        blog_text.setText(Html.fromHtml(current_post.get_blog()));
        Linkify.addLinks(blog_text, Linkify.WEB_URLS);
        blog_text.setTypeface(tf);

        // Image:
            img_loader = new image_loader(second_activity.this);    
            img_view = (ImageView) item_view_second.findViewById(R.id.item_image);
    //      img_view.setTag(current_post.get_image_id());
            img_loader.DisplayImage(current_post.get_image(), img_view);

    //      Log.e("img_view.ID-Tag", "Found:" + img_view.getTag());

        return item_view_second;
    }               
}

This is the part where I detect the url with:

        for (int j = 0; j < counter_blog; j++) {
        if (s != null) {
        s = dis.readLine();
        s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));

        if(s.contains("<p><a href=")) {
        image[j] = s.substring(s.indexOf("<a href=") + 9, s.indexOf(".jpg")+ 4);
        blog[j] = image[j];
        } else {
        blog[j] = s; 
        }
    }
        counter_blog_stop = j + 1;      
}

And over here I use the counter_blog_stop :)

    private void populate_post_list() { 
    for (int i = 0; i < 1; i++) {
        for (int j = 0; j < counter_blog_stop; j++) {
    my_post_second.add(new post_second(title[i], image[j], blog[j], image_id[j]));
        }
    }
}

private void populate_list_view() {
    ArrayAdapter<post_second> adapter = new my_list_adapter_second();
    global.list = (ListView) findViewById(R.id.post_second_list_view);
    global.list.setAdapter(adapter);
}   

I hope my question was clear enough.. When it is not.. Please make a comment, and I'll answer is so soon as possible!

P.S. When I load my activity now, I see the Title on the first position in my listview.. below, an imageview (with a standard image?!) below the Blog, below again a standard image, blog, the whole listview.. Untill there are no Blogs anymore..

Edit: If there are urls detected, they shown perfect in my listView.. But every time there is no image url.. He post an standard imageView.. And I use only a textView called id:item_title and a imagrView called item_image in my listView.

Was it helpful?

Solution 3

Made an array on top:

Integer img_detected[] = new Integer[counter_blog];

I add the two bold lines to my loop (shown below):

for (int j = 0; j < counter_blog; j++) { 
if (s != null) {
s = dis.readLine();
s.substring(s.indexOf("<p>") + 3, s.indexOf("</p>"));

if(s.contains("<p><a href=")) {
image[j] = s.substring(s.indexOf("<a href=") + 9, s.indexOf(".jpg") + 4);
Log.e("TattlerMX Found url", "Found url:" + image[j]);
blog[j] = image[j];
**img_detected[j] = 1;**
} else {
blog[j] = s; 
**img_detected[j] = 0;**
Log.e("TattlerMX Found blog", "Found blog:" + blog[j]);
        }
    }
    counter_blog_stop = j + 1;      
}

change my view class:

// Image:
img_view = (ImageView) item_view_second.findViewById(R.id.item_image);
if(img_detected[position] == 1) {
img_view.setVisibility(View.VISIBLE);
img_loader = new image_loader(second_activity.this);    
img_loader.DisplayImage(current_post.get_image(), img_view);        
} else {
img_view.setVisibility(View.GONE);
}

I used my class int position to read my array.

OTHER TIPS

I'm not sure I totally understand, but I think I can help: Adapters have a built in way to deal with multiple types of views. Since it sounds like you're dealing with two different layouts (text only or text with image), add this to your adapter:

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if (hasImageUrl(position)){
        return 1;
    } else {
        return 0;
    }
}

The hasImageUrl function would check to see if that particular item has an image url. getView() will use this to return the correct type of view to you, so you could write something like this:

@Override
public View getView(int position, View convertView, ViewGroup c) {
    switch (getItemViewType(position)){
       case 0:
         //do your text only stuff
         break;
       case 1:
         //do your image stuff
         break;
    }
    ...

Hope that helps.

how to hide all the imageViews.. Untill I (or my code..) detects an image url (.jpg)?

By default in the layout, set the visibility of ImageView as GONE

 android:visibility="gone"

So, the ImageView will not be visible on the ListView items

When you detect the url for the Image, change the visibility as visible in the Adapter

 img_view.setVisibility(View.VISIBLE);

This will make sure that only the items that have the url will show the ImageView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top