Android AsyncTask中断自定义listview

Android AsyncTask中断自定义listview,android,android-asynctask,progress-bar,Android,Android Asynctask,Progress Bar,嘿,我有一个listview,我使用asynctask从资源中加载它,并在加载列表之前显示进度条 目前,进度条不会在开始时显示,它也会打破列表。任何帮助都将不胜感激,通过打破列表,我的意思是它会像这样一次加载10个元素,而不是一次加载10个元素,这在包含异步任务之前工作正常 活动代码: public class MovieRatingsActivity extends ListActivity { private ArrayList<Movie> movies = new Arra

嘿,我有一个listview,我使用asynctask从资源中加载它,并在加载列表之前显示进度条

目前,进度条不会在开始时显示,它也会打破列表。任何帮助都将不胜感激,通过打破列表,我的意思是它会像这样一次加载10个元素,而不是一次加载10个元素,这在包含异步任务之前工作正常

活动代码:

public class MovieRatingsActivity extends ListActivity
{
 private ArrayList<Movie> movies = new ArrayList<Movie>();
 private LayoutInflater mInflater;


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initializeUI();
}

private void initializeUI()
{
    new LoadList().execute();
}

public class LoadList extends AsyncTask<Void,Void,ArrayList<Movie>>{
    ProgressDialog loading = null;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            loading = new ProgressDialog(getApplicationContext());
            loading.setCancelable(true);
            loading.setMessage("Loading Data...");
            loading.setIndeterminate(true);
            loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            loading.show();
        }
        catch(Exception e){
        Log.d(TAG, "Exception: >> "+e.toString());
        }
    }

    @Override
    protected ArrayList<Movie> doInBackground(Void... aVoid){
        InputStream inputStream = getResources().openRawResource(
                R.raw.ratings);
        movies = Movie.loadFromFile(inputStream);

        return movies;
    }
    @Override
    protected void onPostExecute(ArrayList<Movie> movies) {
        super.onPostExecute(movies);
        loading.hide();
        mInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setListAdapter(new RowIconAdapter(getApplicationContext(), R.layout.listrow, R.id.row_label, movies));
    }
}

//declare a static holder class
public static class ViewHolder{
    ImageView icon;
    TextView movieText,votesText;
}

/** 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)
    {

        //declare a holder object
        ViewHolder holder;
        Movie currMovie = movies.get(pos);

        //nly inflate view once and get all the views once
        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);

            //set the holder class
            convertView.setTag(holder);
        }

        else{
            //get all the views once done
            holder = (ViewHolder) convertView.getTag();
        }

        //set all the values in the list
        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);

        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);
    }
}


}
公共类MovierAtingActivity扩展了ListActivity
{
私有ArrayList电影=新ArrayList();
私人停车场;
/**在首次创建活动时调用*/
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
初始化();
}
private void initializeUI()
{
新建LoadList().execute();
}
公共类LoadList扩展了异步任务{
ProgressDialog加载=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试一试{
加载=新建ProgressDialog(getApplicationContext());
loading.setCancelable(真);
正在加载.setMessage(“正在加载数据…”);
加载。设置不确定(真);
加载.setProgressStyle(ProgressDialog.STYLE\u微调器);
loading.show();
}
捕获(例外e){
Log.d(标记“Exception:>>”+e.toString());
}
}
@凌驾
受保护的ArrayList doInBackground(无效…避免){
InputStream InputStream=getResources().openRawResource(
R.原始评级);
movies=Movie.loadFromFile(inputStream);
返回电影;
}
@凌驾
受保护的void onPostExecute(ArrayList电影){
super.onPostExecute(电影);
loading.hide();
mInflater=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
setListAdapter(新的RowIconAdapter(getApplicationContext(),R.layout.listrow,R.id.row_标签,电影));
}
}
//声明一个静态holder类
公共静态类视图持有者{
图像视图图标;
TextView movieText,votesText;
}
/**自定义行适配器--在电影名称旁边显示一个图标*/
类RowIconAdapter扩展了ArrayAdapter
{
私人电影;
公共RowIconAdapter(上下文c、int-rowResourceId、int-textViewResourceId、,
ArrayList项目)
{
超级(c、rowResourceId、textViewResourceId、items);
电影=物品;
}
公共视图getView(int pos、视图转换视图、视图组父视图)
{
//声明持有者对象
视窗座;
Movie currMovie=movies.get(pos);
//仅对视图进行一次充气,然后一次获取所有视图
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.listrow,父项,false);
holder=新的ViewHolder();
holder.icon=(ImageView)convertView.findViewById(R.id.row_图标);
holder.movieText=(TextView)convertView.findViewById(R.id.row_标签);
holder.votesText=(TextView)convertView.findViewById(R.id.row_subtext);
//设置holder类
convertView.setTag(支架);
}
否则{
//完成所有视图之后
holder=(ViewHolder)convertView.getTag();
}
//设置列表中的所有值
holder.movieText.setText(currMovie.getName());
字符串votesStr=currMovie.getVoices()+“投票”;
holder.votesText.setText(votesStr);
位图movieIcon=getMovieIcon(currMovie.getName(),currMovie.getRating());
holder.icon.setImageBitmap(电影图标);
返回视图;
}
}
/**根据名称和分级创建唯一的电影图标*/
私有位图getMovieIcon(字符串movieName、字符串movieRating)
{
int bgColor=getColor(movieName);
Bitmap b=Bitmap.createBitmap(48,48,Bitmap.Config.ARGB_8888);
b、 橡皮擦颜色(bgColor);//用颜色填充位图
画布c=新画布(b);
油漆p=新油漆();
p、 setAntiAlias(真);
p、 setColor(getTextColor(bgColor));
p、 setTextSize(24.0f);
c、 绘图文本(动画,8,32,p);
返回b;
}
/**从电影名称构造颜色*/
私有int getColor(字符串名称)
{
字符串hex=toHexString(名称);
字符串红色=“#”+十六进制子字符串(0,2);
字符串绿色=“#”+十六进制子字符串(2,4);
字符串blue=“#”+十六进制子字符串(4,6);
字符串alpha=“#”+十六进制子字符串(6,8);
int color=color.argb(整数解码(alpha),整数解码(红色),
Integer.decode(绿色),Integer.decode(蓝色);
返回颜色;
}
/**给定电影名称——从其哈希代码生成十六进制值*/
私有字符串到HexString(字符串名称)
{
int hc=name.hashCode();
字符串十六进制=整数。toHexString(hc);
如果(十六进制长度()<8)
{
十六进制=十六进制+十六进制+十六进制;
hex=hex.substring(0,8);//使用默认颜色值
}
返回十六进制;
}
/**为了获得对比鲜明的颜色而进行的粗略优化还不能很好地工作*/
私有int getTextColor(int bg)
{
INTR=颜色。红色(背景色);
int g=颜色。绿色(背景色);
int b=颜色。蓝色(背景色);
字符串hex=Integer.toHexString(r)+Integer.toHexString(g);
十六进制+=整数。toHexString(b);
int cDec=整数。解码(“#”+hex);
如果(cDec>0xFFFFFF/2)//为较浅的色调选择较暗的颜色
返回颜色.rgb(0,0,0);
其他的
{
r=(r+128)%256;
g=(g+128)%256;
b=(b+128)%256;
返回颜色.rgb(r,g,b);
}
}
}
我的布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@android:id/list" 
    android:layout_height="fill_parent"  
    android:layout_width="match_parent" />
</LinearLayout>

列表行布局:

<?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:orientation="horizontal">
<ImageView android:id="@+id/row_icon" android:padding="2dip"
    android:cacheColorHint="#00000000"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:src="@drawable/icon" />
<LinearLayout android:id="@+id/linearLayout1"
            android:orientation="vertical" 
            android:layout_height="match_parent" 
            android:layout_width="wrap_content">
    <TextView android:id="@+id/row_label" 
 android:layout_width="wrap_content"
        android:layout_height="match_parent" 
        android:text="Terminator 2"
        android:textSize="22sp" />
    <TextView android:id="@+id/row_subtext" 
  android:layout_width="wrap_content"
        android:layout_height="match_parent" 
        android:text="Rating: 5.5, 2340 votes"
        android:textSize="14sp" />          
</LinearLayout>

</LinearLayout>

这就是它的样子:

检查您的布局,我认为问题在于布局。不,在异步任务之前,它工作正常。您可以共享listview行项目布局吗?是的,我同意@developer,可能是布局问题。请提供行项目布局发布缺少方法setListAdapter的完整代码(