列表视图中的第一个progressBar不会进展

时间:2013-12-23 12:16:54

标签: java android android-layout listview android-progressbar

我有一个非常糟糕的问题。 我有一个带适配器的列表视图。

public class MediaAdapter extends  ArrayAdapter<Media> {


public MediaAdapter(Context context, Media[] items) {
    super(context, R.layout.mediaitemlist, items);
}

/* private view holder class */
private class ViewHolder {
    TextView media_name;
    TextView media_path;
    TextView media_version;
    TextView media_type;
    ProgressBar pb;
}   

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    View view = convertView;
    if (view == null) {
        view = (View)LayoutInflater.from(getContext()).inflate(R.layout.mediaitemlist, null);
        holder = new ViewHolder();
        holder.media_name = (TextView) view
                .findViewById(R.id.media_name);
        holder.media_path = (TextView) view
                .findViewById(R.id.media_path);
        holder.media_type = (TextView) view
                .findViewById(R.id.media_type);
        holder.media_version = (TextView) view
                .findViewById(R.id.media_version);
        holder.pb = (ProgressBar)view.findViewById(R.id.itemprogressdl);
        view.setTag(holder);

        //Maybe the problem is here ???????????
        getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));
    } else {
        holder = (ViewHolder) view.getTag();
    }

    Media rowItem = (Media) getItem(position);

    holder.media_name.setText(rowItem.get_Name());
    holder.media_path.setText(rowItem.get_Path());
    holder.media_type.setText(rowItem.get_TypeString());
    holder.media_version.setText("" + rowItem.get_Version());
    rowItem.setmProgressBar(holder.pb);
    return view;
}

}

这里是mediaitemlist布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@drawable/listitembackground" >

<TextView
    android:id="@+id/media_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@android:color/black"
    android:textSize="25sp" />

<TextView
    android:id="@+id/media_path"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/media_name"
    android:textColor="@android:color/black"
    android:visibility="invisible" />

<TextView
    android:id="@+id/media_version"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:textColor="@android:color/black" />

<TextView
    android:id="@+id/media_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="20dp"
    android:layout_toLeftOf="@+id/media_version"
    android:textColor="@android:color/black" />

<ProgressBar
    android:id="@+id/itemprogressdl"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/media_name"
    android:layout_below="@+id/media_name"
    android:layout_toLeftOf="@+id/media_version" 
    android:max="100"
    android:visibility="invisible"
    android:indeterminate="true"/>

</RelativeLayout>

progressBar是媒体类的属性。根据他在listView中的位置,One Media类有自己的进度。

但是,所有progressBar属性都很好,除了listView的第一个。如果我点击第二个或以上,我可以看到progressBar进展,但不是第一个。

那么,为什么第一个progressBar不起作用,下一个工作呢?

如果您需要更多代码,评论或其他内容,请问我。

2 个答案:

答案 0 :(得分:0)

尝试移动此行

getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));

超出if..else条件。

答案 1 :(得分:0)

试试这个答案:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    View view = convertView;
    Media rowItem = (Media) getItem(position);
    if (view == null) {
        view = (View)LayoutInflater.from(getContext()).inflate(R.layout.mediaitemlist, null);
        holder = new ViewHolder();
        holder.media_name = (TextView) view
                .findViewById(R.id.media_name);
        holder.media_path = (TextView) view
                .findViewById(R.id.media_path);
        holder.media_type = (TextView) view
                .findViewById(R.id.media_type);
        holder.media_version = (TextView) view
                .findViewById(R.id.media_version);
        holder.pb = (ProgressBar)view.findViewById(R.id.itemprogressdl);
        view.setTag(holder);

        //Maybe the problem is here ???????????
        getItem(position).setmProgressBar((ProgressBar)view.findViewById(R.id.itemprogressdl));
    } else {
        holder = (ViewHolder) view.getTag();
        rowItem.setmProgressBar(holder.pb);
    }

    holder.media_name.setText(rowItem.get_Name());
    holder.media_path.setText(rowItem.get_Path());
    holder.media_type.setText(rowItem.get_TypeString());
    holder.media_version.setText("" + rowItem.get_Version());

    return view;
}