Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:22:12+00:00 2026-06-10T09:22:12+00:00

I am newbie on android programming, now I met a wired thing on it,

  • 0

I am newbie on android programming, now I met a wired thing on it, I created a listActivity, and the layout of list item is like following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingTop="15dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent" >

        <ImageView
            android:id="@+id/offlineItemIcon"
            android:layout_width="44dp"
            android:layout_height="44dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/user_avatar" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/offlineItemTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="5dp"
                android:text="卷001 2012-08-23 3.0M"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/lightdark"
                android:textSize="18sp" />

            <ProgressBar
                android:id="@+id/offlineLoadingbar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="fill_parent"
                android:layout_height="5dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="15dp"
                android:max="100"
                android:progress="10" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center" >

            <Button
                android:id="@+id/buttonOfflineItemDownload"
                android:layout_width="60dp"
                android:layout_height="44dp"
                android:background="@drawable/xml_button_loadmore"
                android:text="download"
                android:textColor="@color/lightwhite"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@color/commentDate" >
    </LinearLayout>

</LinearLayout>

I made a class to wrap the content of this layout, this one –

public class OfflineItemView extends LinearLayout {

    Context _context = null;

    public Button buttonDownload = null;
    public ProgressBar loadingProgress = null;
    public TextView title = null;
    public ImageView icon = null;

    public String url = null;
    public String filename = null;

    public OfflineItemView(Context context) {
        super(context);
        _context = context;
        init();
    }

    public OfflineItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        _context = context;
        init();
    }

    protected void init() {
        LayoutInflater inflater = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.module_offline_item, this);

        buttonDownload = (Button) findViewById(R.id.buttonOfflineItemDownload);
        buttonDownload.setTag("download");

        loadingProgress = (ProgressBar) findViewById(R.id.offlineLoadingbar);
        loadingProgress.setMax(100);
        loadingProgress.setProgress(0);

        title = (TextView) findViewById(R.id.offlineItemTitle);
        icon = (ImageView) findViewById(R.id.offlineItemIcon);
    }

    public void setTitle(String message) {
        if (message != null) {
            title.setText(message);
        } else {
            title.setText("");
        }
    }

    public void setImageBitmap(Bitmap bitmap) {

        if (bitmap != null) {
            icon.setImageBitmap(bitmap);
        }
    }

    public void setListener(View.OnClickListener listener) {
        this.buttonDownload.setOnClickListener(listener);
    }

    public void setProgress (int i)
    {
        android.util.Log.v ("item", i + "");
        loadingProgress.setProgress(i);
        android.util.Log.v ("item", "current : " + loadingProgress.getProgress());
    }

}

The list working pretty good, it display all item without problem, the screenshot of the list item is –

enter image description here

When the user clicked the button, it will invoke an AsyncTask class to download a file as well as display the progress on item, the download class –

public void downloadOffline(String url, String path,
        OfflineItemView progress) {

    DownloadOfflineAsync downloader = new DownloadOfflineAsync(progress);
    downloader.execute(url, path);
}

class DownloadOfflineAsync extends AsyncTask<String, String, String> {

    private final WeakReference<OfflineItemView> progressbarReference;

    public DownloadOfflineAsync(OfflineItemView progress) {
        progressbarReference = new WeakReference<OfflineItemView>(progress);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            android.util.Log.v("downloadFile", "Lenght of file: "
                    + lenghtOfFile + ":" + aurl[1]);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(aurl[1]);

            try {
                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""
                            + (int) ((total * 100) / lenghtOfFile));
                    output.write(data, 0, count);
                }


            } finally {

                if (output != null) {
                    output.flush();
                    output.close();
                }

                if (input != null) {

                    input.close();
                }
            }
        } catch (Exception e) {

            e.printStackTrace();

        }
        return null;

    }

    protected void onProgressUpdate(String... progress) {
        if (progressbarReference != null) {
            OfflineItemView p = progressbarReference.get();

            //android.util.Log.v ("offline.download", progress[0]);
            p.setProgress(Integer.parseInt(progress[0]));
        }
    }

    @Override
    protected void onPostExecute(String unused) {

        OfflineItemView p = null;

        if (progressbarReference != null) {
            p = progressbarReference.get();
            p.buttonDownload.setTag("browser");
            p.buttonDownload.setText("browse");
            p.requestLayout();
        }

    }
}

And it’s working well, the file is downloaded, and progress is notified to onProgressUpdate, also the loading progress bar on item class is working fine, the progress is set correctly (to print on logcat, and all correct), but the problem is the progress bar never update its indicator, so even the progress is updated from 0 to 100 increasingly, the progress bar doesn’t change anything to show the progress on its UI, so anyone who can give me some advices are appreciated..

PS:

I move the download function to getView (), and seems it’s working great, the progress bar is updating, but in the button event handler is not working, so seems it’s list view problem, how to solve this problem, but?

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        rowView = new OfflineItemView(_context);
        JSONObject item = getItem(position);

        try {
            if (item != null) {
                String t = item.getString(Offline.TAG_NAME) + " "
                        + item.getString(Offline.TAG_DATE) + " "
                        + item.getString(Offline.TAG_SIZE);

                rowView.setTitle(t);

                if (listener == null) {
                    listener = new ItemButtonOnClickListener();
                }

                if (item.optBoolean("dir_exists")) {
                    rowView.buttonDownload.setText("browse");
                    rowView.buttonDownload.setTag("browser");
                }

                rowView.setListener(listener);
                rowView.url = item.getString(Offline.TAG_URL);
                rowView.filename = item.getString(Offline.TAG_FILENAME);

                String imageUrl = item.optString(Offline.TAG_ICON);

                if (imageUrl == null || imageUrl.trim().equals("")) {

                } else {

                    String name = item.optString(Offline.TAG_UNZIP_NAME) + "o";
                    if (name != null) {
                        if (DiskCache.getInstance().fileExists("user_" + name,
                                "user")) {
                            android.util.Log.v("offline", name + ":exists");
                            Bitmap bitmap = DiskCache.getInstance().get(
                                    "user_" + name, "user");
                            if (bitmap != null) {
                                rowView.setImageBitmap(bitmap);
                            }
                        } else {

                            // FileDownloader.getInstance().download(imageUrl,
                            // image, name);
                        }

                    }
                }


            }
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        return rowView;

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:22:13+00:00Added an answer on June 10, 2026 at 9:22 am

    You’re approaching this in kind of a curious way. You said you’re new to Android, and I can’t tell from your comment thread above what your current state of brokenness is. However, I can tell you you’re not following ListView best-practices, and that you might run into some strange issues when you scroll down your list.

    You should use the convertView parameter you’re given. convertView is either null, or a View which you have previously returned from getView. Since you are only ever returning OfflineItemViews, if convertView != null it’s going to be an instance of OfflineItemView, which you can reuse to avoid the overhead of creating a completely new one at draw-time every time a new item is displayed.

    Basically:

    View rowView;
    if(convertView == null){
         rowView = convertView;
    } else {
         rowView = new OfflineItemView(item);
    }
    

    Just be careful with logic like this, because the next time you re-use the view the tags will still be there from the last item that occupied it:

                if (item.optBoolean("dir_exists")) {
                    rowView.buttonDownload.setText("browse");
                    rowView.buttonDownload.setTag("browser");
                }
    

    I.E., you may need an else clause to say rowView.buttonDownload.setText(""); and nullify the tag in the event that dir_exists is false.

    Additionally, (assuming you mean to use something like the commented-out ImageDownloader code you have in getView) if the image doesn’t exist in your image cache, you may spawn off multiple downloads of the same image if getView is called more than once before the image is actually downloaded.

    If you can edit your question to clarify precisely what is broken right now, I can refine this answer to help you further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following thing to achieve in Android(newbie). I have an xml which
I am a complete Android programming newbie. I have completed some tutorial examples like
Hi am newbie to android development. I had created one application where i need
I'm a newbie Android developer. I would like to know if there exists a
I'm a newbie to Android programming and I've been whacking my head over how
I'm a completely newbie to android programming, having done some java for my computing
I have a newbie android question: Why arent strings and other resources like colors
I am a newbie in Android programming and I have this problem: In this
So I am not such a newbie in Programming, Java or Android developing, but
I am a newbie to Android programming. I want two radio buttons that when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.