我正在使用recyclelerview,其中包含 TextView(progresstxt)和Progressbar(downloadprogress)作为夸大项目的一部分。
我需要知道如何从IntentService访问SimpleAdapter中的TextView和Progressbar,以便我可以更新它们的值?
我正在尝试使用 BroadcastReceiver 来实现这一点,但它不起作用,TextView和Progressbar没有更新。
提前谢谢。
我通过onBindViewHolder访问TextView和Progressbar。
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1888)
at android.widget.ListView.makeAndAddView(ListView.java:1857)
at android.widget.ListView.fillDown(ListView.java:682)
at android.widget.ListView.fillFromTop(ListView.java:748)
at android.widget.ListView.layoutChildren(ListView.java:1653)
at android.widget.AbsListView.onLayout(AbsListView.java:2447)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.widget.ScrollView.onLayout(ScrollView.java:2144)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)
通过 IntentService(MoService)完成了几个文件操作,我想从此IntentService 访问/更新 TextView和Progressbar。
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> implements FollowRedirectsCallback {
private final Context mContext;
private String recievedStr;
private int receivedProg;
private MyBroadcastReceiver myBroadcastReceiver;
private MyBroadcastReceiver_Update myBroadcastReceiver_Update;
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
public final TextView title, progresstxt;
public ProgressBar downloadprogress;
public SimpleViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.simple_text);
progresstxt = (TextView) view.findViewById(R.id.progress_text);
downloadprogress = (ProgressBar) view.findViewById(R.id.progressdownload);
}
...
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
recievedStr = intent.getStringExtra(MoService.EXTRA_KEY_UPDATE);
//progresstxt.setText(result); ?
//how to access the TextView in onBindViewHolder?
}
}
public class MyBroadcastReceiver_Update extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
receivedProg = intent.getIntExtra(MoService.EXTRA_KEY_UPDATE_PROGRESS, 0);
//downloadprogress.setProgress(update); ?
//how to access the Progressbar in onBindViewHolder?
}
}
...
@Override
public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
//
myBroadcastReceiver = new MyBroadcastReceiver();
myBroadcastReceiver_Update = new MyBroadcastReceiver_Update();
//register BroadcastReceiver
IntentFilter intentFilter = new IntentFilter(MoService.ACTION_MyIntentService);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
mContext.registerReceiver(myBroadcastReceiver, intentFilter);
IntentFilter intentFilter_update = new IntentFilter(MoService.ACTION_MyUpdate);
intentFilter_update.addCategory(Intent.CATEGORY_DEFAULT);
mContext.registerReceiver(myBroadcastReceiver_Update, intentFilter_update);
//
holder.progresstxt.setText(recievedStr);
holder.downloadprogress.setProgress(receivedProg);
holder.progresstxt.setVisibility(View.VISIBLE);
holder.downloadprogress.setVisibility(View.VISIBLE);
//
}
mContext.unregisterReceiver(myBroadcastReceiver);
mContext.unregisterReceiver(myBroadcastReceiver_Update);
}
答案 0 :(得分:0)
There are a couple of issues with the code. You should not create a new Receiver in your onBindViewHolder method. There should be a single receiver which updates the Item list used by your adapter and call notifyDataSetChanged on the adapter.
Hope it helps