java - E/RecyclerView : No adapter attached;, 数据不想出现在fragment home中,

标签 java android android-fragments android-recyclerview

E/RecyclerView: No adapter attached; skipping layout

Activity_agent.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AgentActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recylcerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="745dp"
    tools:layout_editor_absoluteY="-51dp" />
</RelativeLayout>

moole.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">
    <TextView
        android:id="@+id/idagent"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:text="1"
        android:textSize="30dp"
        android:singleLine="true" />
    <TextView
        android:id="@+id/nomagent"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@+id/idagent"
        android:text="mohamed"
        android:textSize="30dp"
        android:textColor="@color/design_default_color_primary_dark"
        android:singleLine="true" />
</RelativeLayout>

</android.support.constraint.ConstraintLayout>

Agent.java

package com.exemple.android.espacemembre;

public class Agent {
    private int idagent;
    private String nomagent;
    private boolean disponible;
    private int id_admin;

public Agent(int id_agent,String nom_agent) {
    this.idagent=id_agent;
    this.nomagent=nom_agent;
 //   this.disponible=disponible;
  //  this.id_admin=id_admin;
}
public int getIdagent(){
    return idagent;
}
public String getNomagent(){
    return nomagent;
}

}

AgentActivity.java

package com.exemple.android.espacemembre;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

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;

public class AgentActivity extends AppCompatActivity {

//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
    private static final String URL_AGENT = 
     "http://192.168.43.174/php_agent.php";

    //a list to store all the products
    List<Agent> agentList;
      //the recyclerview
   public  RecyclerView recyclerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_agent);
    //getting the recyclerview from xml
    recyclerView = findViewById(R.id.recylcerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //initializing the productlist
    agentList = new ArrayList<>();

    //this method will fetch and parse json
    //to display it in recyclerview
    loadAgent();


}
public void loadAgent(){
    /*
     * Creating a String Request
     * The request type is GET defined by first parameter
     * The URL is defined in the second parameter
     * Then we have a Response Listener and a Error Listener
     * In response listener we will get the JSON response as a String
     * */
    StringRequest stringRequest=new StringRequest(Request.Method.GET, 
URL_AGENT, 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 agent = array.getJSONObject(i);
                    agentList.add(new Agent(
                            agent.getInt("idagent"),
                            agent.getString("nomagent")
                    ));

                }
                AgentAdapter adapter = new 
AgentAdapter(AgentActivity.this, agentList);
                recyclerView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

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

AgentFragment.java

package com.exemple.android.espacemembre;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class AgentFragment extends Fragment {
    View rootView;
    public AgentFragment(){

}
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
       ViewGroup container, @Nullable Bundle savedInstanceState) {


          View rootView= 
         inflater.inflate(R.layout.activity_agent,container,false);

   return rootView;


}
}

代理适配器

      package com.exemple.android.espacemembre;

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView; 

     import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

   import java.util.List;

 public class AgentAdapter extends 
 RecyclerView.Adapter<AgentAdapter.AgentViewHolder> {

private Context g_ctx;
private List<Agent> agentList;

public AgentAdapter(Context g_ctx,List<Agent>agentList){
    this.g_ctx=g_ctx;
    this.agentList=agentList;
}

@NonNull
@Override
public AgentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
  viewType) {
   // LayoutInflater inflater = LayoutInflater.from(g_ctx);
   // View view = inflater.inflate(R.layout.moole, null);
      View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.moole, parent, 
false);

   /* @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
        View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_row, 
parent, false);
        return new ViewHolder(v);*/
    return new AgentViewHolder(v);
}
@Override
public void onBindViewHolder(AgentViewHolder holder, int position) {
    //getting the product of the specified position
    Agent agent = agentList.get(position);
    //binding the data with the viewholder views
    holder.idagent.setText(String.valueOf(agent.getIdagent()));
    holder.nomagent.setText(agent.getNomagent());

}
@Override
public int getItemCount(){
    return agentList.size();
}
class AgentViewHolder extends RecyclerView.ViewHolder{

    TextView idagent ,nomagent;
    public AgentViewHolder(View itemView){
        super(itemView);
        idagent=itemView.findViewById(R.id.idagent);
        nomagent=itemView.findViewById(R.id.nomagent);

    }

}
}

agent.php

<?php 

/*
* Created by Belal Khan
* website: www.simplifiedcoding.net 
* Retrieve Data From MySQL Database in Android
*/

//database constants
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'espace_membre');

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
    echo "Failed to connect to agent: " . mysqli_connect_error();
    die();
}

//creating a query
$stmt = $conn->prepare("SELECT idagent ,nomagent FROM agent;");

//executing the query 
$stmt->execute();

//binding results to the query 
$stmt->bind_result($idagent, $nomagent);

$agent = array(); 

//traversing through all the result 
while($stmt->fetch()){
    $temp = array();
    $temp['idagent'] = $idagent; 
    $temp['nomagent'] = $nomagent; 


    array_push($agent, $temp);
}

//displaying the result in json format 
echo json_encode($agent);
?>`

E/RecyclerView: No adapter attached; skipping layout

数据不想出现在 fragment 中

我是企业管理人员的应用程序,我是代理商信息的恢复者

最佳答案

您必须在设置LayoutManager之后设置AgentAdapter,然后刷新您设置适配器的onResponse数据集。

要刷新数据集,只需在 AgentAdapter 中创建一个小函数,例如:

public void refreshAgents(List<Agent> agentList){
     this.agentList.clear()
     this.agentList = agentList;
     notifyDataSetChanged()
}

关于java - E/RecyclerView : No adapter attached;, 数据不想出现在fragment home中,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125963/

相关文章:

java - 值未通过 Derby 数据库中的prepareStatement 插入

java - 从 JAVA 套接字读取比 Python 慢

java - 无法访问扩展 fragment 的静态嵌套类中的 ListView

java - 如何统计目录中的文件数

java - 初始化大量常量时如何规避 Java 中静态初始化器的大小限制

java - Hibernate - 从 @ManyToMany 删除查询

java - 处理程序随机停止更新 setText

android - 需要帮助连接 Android 手机进行开发

android - 以编程方式解决我的 Android 手机上的通话音频问题?

java - 将搜索查询从主 Activity 传递到 fragment