1

我正在尝试编写一个应用程序,其中从文本文件中提取了许多数据行。这是在我启动应用程序时完成的。当用户单击我的应用程序屏幕中的应用程序时,我想向用户显示进度条。现在有一个短暂的黑屏,然后它显示应用程序数据并显示数据。我想添加一个从 0 到 100 的进度条,或者只是告诉用户我的应用程序正在加载。

任何人都可以帮助我在代码中添加进度条,因为我是新手。我将不胜感激给予我的所有帮助。提前致谢。

public class MovieRatingsActivity extends ListActivity
{


private ArrayList<Movie> movies = new ArrayList<Movie>();

private LayoutInflater mInflater;

private LruCache<String, Bitmap> cache;


/** Called when the activity is first created. */


public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initializeUI();
}

private void initializeUI()
{
    mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
    InputStream inputStream = getResources().openRawResource(   R.raw.ratings);
    movies = Movie.loadFromFile(inputStream);       
    setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
}
static class ViewHolder {
      TextView votesText;
      TextView movieText;
      ImageView icon;
    }

/** Custom row adatper -- that displays an icon next to the movie name */
class RowIconAdapter extends ArrayAdapter<Movie> 
{
    private ArrayList<Movie> movies;        
    public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, 
            ArrayList<Movie> items)
    {
        super(c, rowResourceId, textViewResourceId, items);
        movies  = items;
    }


    public View getView(int pos, View convertView, ViewGroup parent)
    {

        ViewHolder holder; 
        Movie currMovie = movies.get(pos);


        if (convertView == null)
        {

            convertView = mInflater.inflate(R.layout.listrow, parent, false);
            holder = new ViewHolder(); 
            holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);
            holder.movieText = (TextView) convertView.findViewById(R.id.row_label);
            holder.votesText = (TextView) convertView.findViewById(R.id.row_subtext);
            holder.movieText.setText(currMovie.getName());
            String votesStr = currMovie.getVotes()+" votes";
            holder.votesText.setText(votesStr);
            Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
            holder.icon.setImageBitmap(movieIcon);
            Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName());
        }


            return convertView;
    }
}

/** Creates a unique movie icon based on name and rating */
private Bitmap getMovieIcon(String movieName, String movieRating)
{
    int bgColor = getColor(movieName);
    Bitmap b = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
    b.eraseColor(bgColor); // fill bitmap with the color
    Canvas c = new Canvas(b);
    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setColor(getTextColor(bgColor));
    p.setTextSize(24.0f);
    c.drawText(movieRating, 8, 32, p);
    return b;
}

/** Construct a color from a movie name */
private int getColor(String name)
{
    String hex = toHexString(name);
    String red = "#"+hex.substring(0,2);
    String green = "#"+hex.substring(2,4);
    String blue = "#"+hex.substring(4,6);
    String alpha = "#"+hex.substring(6,8);
    int color = Color.argb(Integer.decode(alpha), Integer.decode(red), 
                            Integer.decode(green), Integer.decode(blue));
    return color;
}

/** Given a movie name -- generate a hex value from its hashcode */
private String toHexString(String name)
{
    int hc = name.hashCode();
    String hex = Integer.toHexString(hc);
    if (hex.length() < 8)
    {
        hex = hex+hex+hex;
        hex = hex.substring(0,8); // use default color value
    }
    return hex;
}

/** Crude optimization to obtain a contrasting color -- does not work well yet */
private int getTextColor(int bg)
{

    int r = Color.red(bg);
    int g = Color.green(bg);
    int b = Color.blue(bg);
    String hex = Integer.toHexString(r)+Integer.toHexString(g);
    hex += Integer.toHexString(b);

    int cDec = Integer.decode("#"+hex);
    if (cDec > 0xFFFFFF/2)  // go dark for lighter shades
        return Color.rgb(0, 0, 0);
    else
    {
        r = (r+128)%256;
        g = (g+128)%256;
        b = (b+128)%256;
        return Color.rgb(r,g,b);
    }
}
}
4

2 回答 2

0

http://android-er.blogspot.be/2010/11/progressbar-running-in-asynctask.html

onPostExecute ==> 读取完成,下一步该做什么

onPreExecute ==> 开始阅读首先要做什么

doInBackground ==> 读取数据

onProgressUpdate ==> 更新进度条

有了这个你应该能够创建一个进度条

于 2013-05-21T12:44:46.077 回答
0

将进度条添加到您的 UI。然后移动这个:

  movies = Movie.loadFromFile(inputStream);       
  setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));

使用更新进度条的方法进入doInBackground()AsyncTask 。onProgressUpdate()onPostExecute中,将进度条替换为实际内容。

要实际报告进度,您可能需要向该Movie.loadFromFile()方法添加一个侦听器,以便在需要更新进度条时获得回调。

于 2013-05-21T12:46:47.050 回答