简体   繁体   中英

First progressBar in listview doesn't progress

I have a very bad issue. I have a listview with an adapter.

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

}

Here, the mediaitemlist Layout :

<?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>

The progressBar is an attribute of a media class. One Media class have is own progress according to his place in the listView.

But, all the progressBar attributes are good, except the first of the listView. If I click on the second or over, I can see the progressBar progresses, but not with the first.

So, why the first progressBar doesn't work and the next work?

If you need more code,comments or other, ask me.

try moving this line

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

out of the if..else condition.

Try this answer:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM