没有附加适配器,跳过reyclerview和json片段的布局

时间:2015-12-09 14:25:09

标签: java android json android-fragments android-adapter

我确信json的一切都还可以,但我认为我并没有将setAdapter()定位在好地方。所以我在这里犯了一个错误?或者我在其他地方犯了一个错误。任何帮助,将不胜感激。 这是代码:

@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();
    }
}

1 个答案:

答案 0 :(得分:1)

您的片段视图是使用onCreateView方法创建的,&amp;由于您在视图创建期间未在RecyclerView上设置适配器,因此会出现红色错误行:未附加适配器,跳过布局

尝试将适配器设置为RecyclerView方法中的onCreateView:传入空礼品数组:

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

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