带有进度条的 Android 自定义适配器

标签 android progress-bar android-arrayadapter

我有一个数组列表的自定义适配器。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal">   

 <ProgressBar android:id="@+id/downloadprogress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50"
    android:secondaryProgress="75" />
 <TextView android:id="@+id/tripsname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


</LinearLayout>

但是当我尝试访问适配器中的进度条时

  private class MyAdapter extends ArrayAdapter<Trip> {

    int resource; 
    public MyAdapter(Context context, int resource, ArrayList<Trip> items) { 
        super(context, resource,items); 
        this.resource=resource; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
        LinearLayout tripListItemView; 
        final Trip t = getItem(position); 
        String name = t.getName();
        boolean offline = t.isOffline() ;
        if(convertView==null) { 
            tripListItemView = new LinearLayout(getContext()); 
            String inflater = Context.LAYOUT_INFLATER_SERVICE; 
            LayoutInflater vi; 
            vi = (LayoutInflater)getContext().getSystemService(inflater); 
            vi.inflate(resource, tripListItemView, true); 
        } 
        else { 
            tripListItemView = (LinearLayout) convertView; 
        } 
        setProgressBarVisibility(true);
        View v = findViewById(R.id.downloadprogress);
        ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.downloadprogress);

它总是返回 null,就像它没有找到进度条一样。但是,进度条显示在列表中,但我需要在适配器内部访问它。

有人知道解决办法吗?

谢谢

最佳答案

您的代码中的奇怪之处在于 findViewById()ArrayAdapter 下无法访问...我不明白您的代码如何编译...

顺便说一句,您应该使用在 getView(...) 的参数中传递的 View 来检索您的进度条。

ProgessBar progressBar = (ProgressBar) convertView.findViewById( R.id.downloadprogressbar );

关于带有进度条的 Android 自定义适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333295/

相关文章:

android - 暂时禁用android中的互联网访问?

android - 如何使用具有多个输入分配的 RenderScript?

java - Spinner onItemSelectedListener 不起作用?

android - fragment 中的 ListView setAdapter,空指针异常

android - 我的设备未显示“允许 USB 调试”对话框

android - Android Studio Gradle同步无法解决

html - 为什么 progress 元素在高度缩小的情况下仍占据相同的垂直空间?

java - 如何在 Java 中更新 Swing JProgressBar

android - 一段时间后关闭进度条?

android - 从 ArrayAdapter 刷新 ListView