Show resulting data hereNow Pubnub in recycleView android

I am calling this method when I start Activity. I want it to show up in RecycleView this code of mine and give me an exception

public class FirstFragment extends fragment {

//private
String[] username = {"joe", "mido", "star", "fawzy", "mohsen"};
private RecyclerView recyclerView;
TextView textView;
ProgressDialog dialog;
FirstpageAdapter adapter;
View layout;
private final Handler myHandler = new Handler();
List<String> ids = new ArrayList<>();
private final static String TAG = "FIRST_FRAGMENT";
ListView listView;
Runnable updateRunnable;

public FirstFragment() {
    // Required empty public constructor
}

public List<String> getData() {
    List<String> data = new ArrayList<>();
    for (int i = 0; i < username.length; i++) {
        data.add(username[i]);
    }
    return data;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //new MyTask().execute();
    View layout = inflater.inflate(R.layout.fragment_first, container, false);
    recyclerView=(RecyclerView)layout.findViewById(R.id.firstList);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    PubNubService.herNow(new Callback() {
        public void successCallback(String channel, Object message) {
            try {
                JSONObject object = new JSONObject(message.toString());
                JSONArray jsonArray = object.getJSONArray("uuids");
                for (int i = 0; i < jsonArray.length(); i++) {
                    ids.add(jsonArray.getString(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.e(TAG, ids.size() + "");
            Log.e(TAG, message.toString());

                    adapter = new FirstpageAdapter(getActivity(), ids);
            recyclerView.setAdapter(adapter);

        }

        @Override
        public void errorCallback(String s, PubnubError pubnubError) {
            Log.e(TAG, "error Callback" + pubnubError.getErrorString());
        }
    });

    return layout;
}

      

}

An exception

08-01 14: 16: 06.903 12365-12490 / com.sprintone E / AndroidRuntime: FATAL EXCEPTION: Non-Subscribe-Manager-1384510668-5 Process: com.sprintone, PID: 12365 android.view.ViewRootImpl $ CalledFromWrongThreadException: original only the thread that created the view hierarchy can touch its views. at android.view.ViewRootImpl.checkThread (ViewRootImpl.java:6024) on android.view.ViewRootImpl.requestLayout (ViewRootImpl.java:820) on android.view.View.requestLayout (View.java:16431) on android.view. View.requestLayout (View.java:16431) on android.view.View.requestLayout (View.java:16431) on android.view.View.requestLayout (View.java:16431) on android.view.View.requestLayout (View .java: 16431) at android.view.View.requestLayout (View.java:16431) at android.support.v4.widget.DrawerLayout.requestLayout (DrawerLayout.java:979) on android.view.View.requestLayout (View.java:16431) on android.view.View.requestLayout (View.java:16431) on android.view.View .requestLayout (View.java:16431) on android.view.View.requestLayout (View.java:16431) on android.support.v7.widget.RecyclerView.requestLayout (RecyclerView.java:2245) on android.support.v7 .widget.RecyclerView.setAdapter (RecyclerView.java:564) at com.sprintone.userInterface.Fragment.FirstFragment $ 1.successCallback (FirstFragment.java:101) at com.pubnub.api.PubnubCore.invokeCallback (Unknown Source) pubnub.api.PubnubCore.invokeCallback (Unknown source) at com.pubnub.api.PubnubCore $ 11.handleResponse (Unknown source) at com.pubnub.api.NonSubscribeWorker.process (Unknown Source) at com.pubnub.api.Worker.run (Unknown Source) at java.lang.Thread.run (Thread.java:841)

+3


source to share


1 answer


As stated here Only the original thread that created a view hierarchy can touch its views.

you cannot access ui element from outside the main thread, try using AsyncTask

or RunOnUiThread



Obviously PubNub callbacks work on a background thread, because in android you cannot work like fetching data from a remote server in the main thread to avoid app crashing in the request and you are trying to access RecucleView

from the pubnub call response
it is better to get data in the pubnub callback and then set it in your view outside the callback

or you can delegate to get the date from the pubnub callback when it's ready there is a useful link here
link1 link2

+2


source







All Articles