带进度栏的 Android 自定义适配器

发布于 2024-08-22 11:58:05 字数 1966 浏览 5 评论 0原文

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

<?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,就像它找不到进度条一样。但是,进度条显示在列表中,但我需要在适配器内访问它。

有人知道解决办法吗?

谢谢

i have a custom adapter for an arraylist.

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

but when i try to access to the progress bar in the adapter

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

it always return null, like it doesn't find the progress bar. However, the progress bar is shown in the list, but i need to access to it inside the adapter.

Anybody knows the solution?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-08-29 11:58:05

您的代码中奇怪的事情是 findViewById()ArrayAdapter 下无法访问...我不明白您的代码如何编译...

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

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

The strange thing in your code is that findViewById() isn't accessible under ArrayAdapter... I don't understand how your code can compile...

By the way, you should use the view passed in the parameters of getView(...) to retrieve your progress bar.

ProgessBar progressBar = (ProgressBar) convertView.findViewById( R.id.downloadprogressbar );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文