java—单击按钮获取api后,如何在片段中显示recyclerview

gz5pxeao  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(259)

在搜索栏中搜索配方并单击搜索片段中的搜索按钮之后,我试图显示一些配方数据。我在搜索片段中使用回收器视图来显示搜索栏和按钮下面的数据。
下面是fragment\u search.xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.SearchFragment">

    <!-- TODO: Update blank fragment layout -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="-230dp"
            android:src="@drawable/home_oval" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="FOODIES"
                android:textSize="40dp"
                android:layout_centerHorizontal="true"/>

        </RelativeLayout>

        <SearchView
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dp"
            android:queryHint="Find your today's recipe"
            android:iconifiedByDefault="false"
            android:background="@drawable/searchoval"
            android:id="@+id/searchbar"
            />
        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="search"
            android:background="@drawable/rounded_btn"
            android:layout_below="@+id/searchbar"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:id="@+id/button1"/>

        <ImageView
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_man_searching_location_using_gps_2127154_0"
            android:layout_below="@+id/button1"
            android:id="@+id/searching_logo"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searching_text"
            android:text="Search for yummy delicacies today"
            android:layout_centerHorizontal="true"
            android:textAlignment="center"
            android:fontFamily="@font/quicksand"
            android:layout_below="@+id/searching_logo"
            android:textSize="25dp"
            android:textColor="@color/black"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/button1"
            android:id="@+id/recycler_view"
            >

        </androidx.recyclerview.widget.RecyclerView>

    </RelativeLayout>

</FrameLayout>

这是我的searchfragment.java文件。

package com.example.recipeappandroid.Fragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.recipeappandroid.Adapter.RecipeAdapter;
import com.example.recipeappandroid.Model.Recipe;
import com.example.recipeappandroid.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class SearchFragment extends Fragment {

    Button click;
    //public static TextView fetchedText;
    ImageView searching_logo;
    TextView searching_text;
    SearchView searchbar;
    String query="";
    RecyclerView recyclerView;
    public static ArrayList<Recipe> recipeList;
    public static RecipeAdapter recipeAdapter;
    private  RequestQueue mRequestQueue;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search, container, false);

        click = (Button) view.findViewById(R.id.button1);
        //fetchedText = (TextView) view.findViewById(R.id.fetcheddata);
        searchbar = (SearchView) view.findViewById(R.id.searchbar);
        searching_logo = view.findViewById(R.id.searching_logo);
        searching_text = view.findViewById(R.id.searching_text);
        recyclerView = view.findViewById(R.id.recycler_view);

        recyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        //recipeAdapter = new RecipeAdapter();
        recyclerView.setAdapter(recipeAdapter);
        recipeList = new ArrayList<>();
        //getData();

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                query = searchbar.getQuery().toString();
                String url = "http://localhost:5000/" + query;
                JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject jsonObject = response.getJSONObject(i);
                                JSONObject recipes = jsonObject.getJSONObject("recipe");
                                //Recipe recipe = new Recipe();
                                String recipe_img = recipes.getString("image");
                                String recipe_title = recipes.getString("label");
                                String recipe_data =  recipes.getString("source");
                                recipeList.add(new Recipe(recipe_img,recipe_title,recipe_data));
                            }
                            recipeAdapter = new RecipeAdapter(getContext(), recipeList);
                            //recyclerView.setAdapter(recipeAdapter);
                            recipeAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //Toast.makeText(SearchFragment.this,"Error Occured",Toast.LENGTH_SHORT).show();
                        error.printStackTrace();
                    }
                });
                mRequestQueue = Volley.newRequestQueue(getContext());
                mRequestQueue.add(jsonArrayRequest);
               /* Log.d("QUEEEERRRYYYY",query);
                ApiCall process = new ApiCall(searching_logo,searching_text);
                process.execute(query);*/

            }
        });

        return view;

    }

}

我喜欢api,在onclick监听器中设置arraylists和adapter,但是我一直得到“e/recyclerview:no adapter attached;正在跳过logcat中的“布局”错误。
下面是适配器的代码。

package com.example.recipeappandroid.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recipeappandroid.Model.Recipe;
import com.example.recipeappandroid.R;
import com.example.recipeappandroid.Viewholder.recipeViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class RecipeAdapter extends RecyclerView.Adapter<recipeViewHolder>{
    private Context mContext;
    private ArrayList<Recipe> mRecipe;

    public RecipeAdapter(Context context,ArrayList<Recipe> recipe) {
        mContext = context;
        mRecipe = recipe;
    }

    public void setData(ArrayList<Recipe> mRecipe) {
        this.mRecipe = mRecipe;
    }

    @NonNull
    @Override
    public recipeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_row, viewGroup, false);
        return new recipeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull recipeViewHolder viewHolder, int i) {
        Recipe recipe = mRecipe.get(i);
        Picasso.get().load(recipe.getImg()).into(viewHolder.image);
        viewHolder.recipe_title.setText(recipe.getTitle());
        viewHolder.recipe_data.setText(recipe.getData());
    }

    @Override
    public int getItemCount() {
        return mRecipe.size();
    }
}
mv1qrgav

mv1qrgav1#

这是我的解决办法

//recipeAdapter = new RecipeAdapter();
recyclerView.setAdapter(recipeAdapter);
recipeList = new ArrayList<>();

//change code:
recipeList = new ArrayList<>();
recipeAdapter = new RecipeAdapter(getContext(), recipeList);
recyclerView.setAdapter(recipeAdapter);

//after get data in API:
//recipeAdapter = new RecipeAdapter(getContext(), recipeList);
//recyclerView.setAdapter(recipeAdapter);
recipeAdapter.notifyDataSetChanged();

相关问题