【问题标题】:How to update ProgressBar with respect to downloaded data while downloading text data from web using Asynctask in android如何在android中使用Asynctask从Web下载文本数据时针对下载的数据更新ProgressBar
【发布时间】:2015-03-01 08:06:16
【问题描述】:

我想显示从网上下载的数据的进度条。我是必须动态创建还是必须通过在 xml 中创建它来通过 Id 引用。我有点困惑。这是我的代码。你能告诉我怎么做吗?

package com.example.telugumovies;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends ListActivity {
String category ="";
String[] cat = {"Upcoming","Telugu"};
private List<String> categories ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    categories = new ArrayList<String>();
    int i=0;
    for(i=0;i<2;i++)
    {
        categories.add(cat[i]+" Movies");
    }
    ArrayAdapter<String> myAdapter = new ArrayAdapter <String>(this, 
            R.layout.listview_rowlayout, R.id.tv, categories);

     // assign the list adapter
     setListAdapter(myAdapter);
}
 // when an item of the list is clicked
@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
    super.onListItemClick(list, view, position, id);
    Bundle giveCategory = new Bundle();
    if(position == 0)
    {
        try {
            new GetData().execute();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
        }
    }
    else if(position == 1)
    {
        giveCategory.putString("cate", cat[position]);
        Intent a = new Intent(MainActivity.this,Show_Years.class);
        a.putExtras(giveCategory);
        startActivity(a);   

    }
    else
    {
        Toast.makeText(getApplicationContext(), "Sorry This option is not Available for Now ",
               Toast.LENGTH_SHORT).show();
    }
    //String selectedItem = (String) getListView().getItemAtPosition(position);


}

class GetData extends AsyncTask<String,Integer,String>
{
    ProgressBar pb ;
    protected void onPreExecute()
    {
        pb = new ProgressBar(MainActivity.this);
        pb.setVisibility(View.VISIBLE);
    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String alldata = "";
        int flag=1;
        String data = "";
          URL url = null;
          int i=0,j=0,count=0,c=0;
          URLConnection con = null;
          InputStream is = null;
          String line = null;
        try
        {
            url = new URL("http://www.filmibeat.com/telugu/upcoming-movies.html");
            con = url.openConnection();
            is = con.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
               //System.out.println(line);
                alldata = "";
                flag=0;
                if(line.contains("<h3"))
                {j=0;
                flag=1;
                    for(i=0;i<line.length();i++)
                {
                    if(line.charAt(i)=='<')
                    {
                        j=1;
                    }
                    else if(j==1 && line.charAt(i)=='>')
                    {
                        j=0;
                    }
                    else if(j==0)
                    {
                        alldata = alldata + (line.charAt(i));
                    }
                }
                }
                data=data+alldata;
                if(flag==1)
                {
                data=data+"$";
                }

        }
            System.out.println(data);
            return data;
        }
            catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {
            return data;
        }
        }
    protected void onPostExecute(String someting)
    {
        Bundle givedata = new Bundle();
        givedata.putString("moviedata",someting);
        Intent a = new Intent(MainActivity.this,Show_upcomingmovies.class);
        a.putExtras(givedata);
        pb.setVisibility(View.INVISIBLE);
        startActivity(a);
    }
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

【问题讨论】:

    标签: android android-asynctask android-progressbar


    【解决方案1】:

    使用动态创建的进度条或从 id 引用它都可以。

    使用来自 XML 的引用可以让您更好地控制进度条的外观,因为您可以根据需要专门设计它(例如外观、必须出现的位置等)。但如果不是这种情况,您也可以像在当前代码中一样使用动态创建的。

    【讨论】:

      【解决方案2】:

      有很多方法。一种是将 ProgressDiaolog 定义为 AsyncTask 中的一个字段,然后将其添加到您的 onPreExecuted() 中:

      pDialog = new ProgressDialog(this);
      pDialog.setMessage("some downloading massage");
      pDialog.setIndeterminate(false);
      pDialog.setMax(100); //you can adjust this
      pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      pDialog.setCancelable(false);
      pDialog.show();
      

      然后您可以通过在 AsyncTask 中覆盖 updateProgress() 并调用 pDialog.setProgress() 来更新进度;

      【讨论】:

      • 嗯,他问的是 ProgressBar,而不是 ProgressDialog。
      • 是的,你完全正确,但没有太大区别。您可以在 ProgressBar 中替换它并将其添加到您的布局中(他没有做的)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多