【问题标题】:First progressBar in listview doesn't progresslistview中的第一个progressBar没有进展
【发布时间】:2013-12-23 12:16:53
【问题描述】:

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

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 是一个媒体类的属性。一个 Media 类根据他在 listView 中的位置有自己的进度。

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

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

如果您需要更多代码、cmets 或其他,请询问我。

【问题讨论】:

  • 你试过holder.pb.setProgress()
  • 不,因为我像这样更新第三类的进度:oneMedia.getProgressBar().setProgress();它适用于第二个项目,但不适用于第一个项目,我不知道为什么......

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


【解决方案1】:

试着移动这条线

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

超出 if..else 条件。

【讨论】:

    【解决方案2】:

    试试这个答案:

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

    【讨论】:

    • 我不能调用 rowItem,它是在条件之后定义的......或者我移动 Media rowItem = (Media) getItem(position);在条件之前?
    • 其实第一次所有progressBar都是null。为什么?
    • 只是progressBar还是textview??
    • 对不起,我弄错了,progressBars 不是 null,但我看不到它们,就像屏幕外的一样
    • 只是progressBar,所有的textView都设置好了,都是正确的
    猜你喜欢
    • 2017-02-02
    • 2018-04-05
    • 2012-04-26
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多