带进度栏的 Android 自定义适配器
我有一个用于数组列表的自定义适配器。
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中奇怪的事情是
findViewById()
在ArrayAdapter
下无法访问...我不明白您的代码如何编译...顺便说一句,您应该使用
getView(...)
参数中传递的视图来检索进度条。The strange thing in your code is that
findViewById()
isn't accessible underArrayAdapter
... 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.