recyclerviewadapter未使用片段附加

时间:2019-04-04 15:33:20

标签: java android android-fragments android-recyclerview recycler-adapter

我试图使用recyclerview从数据库检索数据到片段,但出现错误RecyclerView:未附加适配器;跳过布局

我试图在getactivity和getcontext之间进行切换,但仍然无法解决任何问题


search.java


package com.example.lifetravel40;

public class Search {

private  int search_id;
private String activity;
private String description;
private String activity_img;
private String origin;
private String destination;
private String date_from;
private String date_to;
private String price ;

public Search(String activity, String description, String activity_img, String origin, String destination, String date_from, String date_to, String price){

    this.search_id = search_id;
    this.activity = activity;
    this.description = description;
    this.activity_img = activity_img;
    this.origin =origin;
    this.destination = destination;
    this.date_from = date_from;
    this.date_to = date_to;
    this.price = price;
}

public int getSearch_id(){
    return search_id;
}

public String getActivity(){
    return activity;
}


public String getDescription(){
    return description;
}


public String getActivity_img(){
    return activity_img;
}


public String getOrigin(){
    return origin;
}

public String getDestination(){
    return destination;
}


public String getDate_from(){
    return date_from;
}


public String getDate_to(){
    return date_to;
}


public String getPrice(){
    return price;
}
  }

searchadapter.java


 package com.example.lifetravel40;

 import android.content.Context;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.TextView;

 import com.bumptech.glide.Glide;

 import java.util.List;

 public class SearchAdapter extends 
 RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {

private Context mCtx;
private List<Search> searchList;

public SearchAdapter(Context mCtx, List<Search> searchList){

    this.mCtx = mCtx;
    this.searchList = searchList;
}


@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.redirect_item_row, null);

    return new SearchViewHolder(view);
}

    @Override
public void onBindViewHolder(SearchViewHolder holder, int position){
    Search search = searchList.get(position);

    Glide.with(mCtx)
            .load(search.getActivity_img())
            .into(holder.activity_img);

    holder.activity.setText(search.getActivity());
    holder.description.setText(search.getDescription());
    holder.origin.setText(search.getOrigin());
    holder.destination.setText(search.getDestination());
    holder.date_from.setText(search.getDate_from());
    holder.date_to.setText(search.getDate_to());
    holder.price.setText(search.getPrice());


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

  class SearchViewHolder extends RecyclerView.ViewHolder{

    TextView activity, description, origin, destination, date_from, date_to, 
  price;
    ImageView activity_img;

    public SearchViewHolder(View itemView){
        super(itemView);

        activity = itemView.findViewById(R.id.activity);
        description = itemView.findViewById(R.id.description);
        origin = itemView.findViewById(R.id.origin);
        destination = itemView.findViewById(R.id.destination);
        date_from = itemView.findViewById(R.id.date_from);
        date_to = itemView.findViewById(R.id.date_to);
        price = itemView.findViewById(R.id.price);
        activity_img = itemView.findViewById(R.id.activity_img);

    }
}
} 

redirectfragment.java


 package com.example.lifetravel40;


 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;

 import com.android.volley.Request;
 import com.android.volley.Response;
 import com.android.volley.VolleyError;
 import com.android.volley.toolbox.StringRequest;
 import com.android.volley.toolbox.Volley;

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

 import java.util.ArrayList;
 import java.util.List;


  /**
 * A simple {@link Fragment} subclass.
 */
 public class redirectFragment <recyclerView> extends Fragment  {


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

private static final String URL_Seacrch ="http://192.168.56.1/android/search.php";

List<Search> searchList;

RecyclerView recylcerView ;





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

    recylcerView =(RecyclerView) v.findViewById(R.id.recyclerView);
    LinearLayoutManager manager = new LinearLayoutManager(getContext());
    recylcerView.setLayoutManager(manager);
    recylcerView.setHasFixedSize(true);

    searchList = new ArrayList<>();

    loadSearch();




    return v;

}


public void loadSearch() {

    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_Seacrch, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONArray array = new JSONArray(response);

                for (int i = 0; i < array.length(); i++) {

                    JSONObject search = array.getJSONObject(i);

                    searchList.add(new Search(
                            search.getString("activity"),
                            search.getString("description"),
                            search.getString("activity_img"),
                            search.getString("origin"),
                            search.getString("destination"),
                            search.getString("date_from"),
                            search.getString("date_to"),
                            search.getString("price")
                            ));
                }


               SearchAdapter adapter1 = new SearchAdapter(getContext(),searchList);
                recylcerView.setAdapter(adapter1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }

});

    Volley.newRequestQueue(getActivity()).add(stringRequest);
}


}

我真的迷失了方向,因为我找不到我哪里出了错

2 个答案:

答案 0 :(得分:0)

尝试在adapter的{​​{1}}中设置空的recyclerView

onCreateView()

然后在您将新项目添加到@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_redirect, container, false); recylcerView =(RecyclerView) v.findViewById(R.id.recyclerView); LinearLayoutManager manager = new LinearLayoutManager(getContext()); recylcerView.setLayoutManager(manager); recylcerView.setHasFixedSize(true); searchList = new ArrayList<>(); List<Search> searchList = new ArrayList<>() SearchAdapter adapter1 = new SearchAdapter(getContext(),searchList); recylcerView.setAdapter(adapter1); loadSearch(); return v; } 调用searchList

中时作出响应

答案 1 :(得分:0)

尝试更改

public class redirectFragment <recyclerView> extends Fragment

进入此

public class redirectFragment extends Fragment