简体   繁体   中英

No adapter attached, skipping layout with reyclerview and json in fragment

Everything is okay with the json i'm sure, but i think that i'm not positioning setAdapter() on good place. So where i'm making here a mistake? Or i'm making somewhere else a mistake. Any help would be appreciated. This is the code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.gifts_activity, container, false);
    dbh = new DBHelper(getActivity());
    avLoadingIndicatorView = (AVLoadingIndicatorView)
            v.findViewById(R.id.avloadingIndicatorView);
    recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity().getResources()));

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    giftItemSorter = new GiftItemSorter();
    int birthdayId = getActivity().getIntent().getIntExtra(EditBirthday.KEY_EXTRA_BIRTHDAY_ID, 0);
    Cursor rs = dbh.getBirthdayId(birthdayId);
    rs.moveToFirst();

    new AsyncHttpTask().execute(url);

    if (!rs.isClosed()) {
        rs.close();}

    return v;
}

void startAnim(){
    avLoadingIndicatorView.setVisibility(View.VISIBLE);
}

void stopAnim(){
    avLoadingIndicatorView.setVisibility(View.GONE);
}

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {

    @Override
    protected void onPreExecute() {
        startAnim();
    }

    @Override
    protected Integer doInBackground(String... params) {
        Integer result = 0;
        HttpURLConnection urlConnection;
        try {
            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int statusCode = urlConnection.getResponseCode();

            // 200 represents HTTP Ok
            if (statusCode == 200) {
                BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = r.readLine()) != null) {
                    response.append(line);
                }
                parseResult(response.toString());
                result = 1; // Successful
            } else {
                result = 0; //"Failed to fetch data!";
            }
        } catch (Exception e) {
            Log.d(TAG, e.getLocalizedMessage());
        }
        return result; //"Failed to fetch data!";
    }
    @Override
    protected void onPostExecute(Integer result) {
        // Download complete. Let us update UI
        stopAnim();

        if (result == 1) {
            giftListAdapter = new GiftListAdapter(getActivity(), gifts);
            reyclerView.setAdapter(giftListAdapter);
        } else {
            SuperActivityToast.create(getActivity(), getString(R.string.err_msg_no_internet),
                    SuperToast.Duration.SHORT, Style.getStyle(Style.RED, SuperToast.Animations.FLYIN)).show();
        }
    }
}

private void parseResult(String result) {
    try {
        JSONObject response = new JSONObject(result);
        JSONArray movies = response.optJSONArray("movies");
        gifts = new ArrayList<>();

        for (int i = 0; i < movies.length(); i++) {
            JSONObject post = movies.optJSONObject(i);
            GiftItem item = new GiftItem();
            item.setThumbnail(post.optString("image"));
            item.setTitle(post.optString("title"));
            item.setDescription(post.optString("description"));
            item.setSource(getString(R.string.source) + " " + post.optString("source"));
            item.setTotalRating(post.optInt("rating"));
            item.setPrice(post.optDouble("price"));

            gifts.add(item);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Your fragment's view is created in the onCreateView method, & since you're not setting the adapter on the RecyclerView during its view creation, you get that red error line : No adapter attached, skipping layout .

Try to set an adapter to the RecyclerView in the onCreateView method: pass in an empty gifts array:

// Supposing that you declared the gifts arraylist as a member variable
gifts = new ArrayList<>();

giftListAdapter = new GiftListAdapter(getActivity(), gifts);
reyclerView.setAdapter(giftListAdapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM