Java E/RecyclerView:未连接适配器;,数据不希望出现在片段主目录中,
E/RecyclerView:未连接适配器;跳过布局 Activity\u agent.xmlJava E/RecyclerView:未连接适配器;,数据不希望出现在片段主目录中,,java,android,android-fragments,android-recyclerview,Java,Android,Android Fragments,Android Recyclerview,E/RecyclerView:未连接适配器;跳过布局 Activity\u 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="ht
<?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>
<?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>
AgentActivity.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;
}
}
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);
}
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;
}
}
AgentAdapter
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);
}
}
}
package com.example.android.espacemembre;
导入android.content.Context;
导入android.support.annotation.NonNull;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
导入java.util.List;
公共类AgentAdapter扩展
RecyclerView.适配器{
私有上下文g_ctx;
私人名单代理名单;
公共代理适配器(上下文g_ctx,列表代理列表){
这个.g_ctx=g_ctx;
this.agentList=agentList;
}
@非空
@凌驾
public AgentViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int
视图类型){
//LayoutInflater充气机=LayoutInflater.from(g_ctx);
//视图=充气机。充气(R.layout.moole,空);
视图v=
LayoutInflater.from(parent.getContext()).flate(R.layout.moole,parent,
假);
/*@覆盖
public ViewHolder onCreateViewHolder(视图组父级,int-viewType)
{
视图v=
LayoutFlater.from(parent.getContext())。充气(R.layout.component\u行,
父母,假);
返回新的视图持有者(v)*/
返回新的AgentViewHolder(v);
}
@凌驾
BindViewHolder上的公共无效(代理视图持有者,内部位置){
//获取指定位置的产品
Agent=agentList.get(位置);
//将数据与viewholder视图绑定
holder.idagent.setText(String.valueOf(agent.getIdagent());
holder.nomagent.setText(agent.getNomagent());
}
@凌驾
public int getItemCount(){
return agentList.size();
}
类AgentViewHolder扩展了RecyclerView.ViewHolder{
TextView-idagent、nomagent;
公共代理查看文件夹(查看项目视图){
超级(项目视图);
idagent=itemView.findViewById(R.id.idagent);
nomagent=itemView.findviewbyd(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:未连接适配器;跳过布局
数据不希望出现在片段中
您必须在设置LayoutManager后设置AgentAdapter,然后在设置适配器的响应上刷新数据集 要刷新数据集,只需在AgentAdapter中创建一个小函数,如:
public void refreshAgents(List<Agent> agentList){
this.agentList.clear()
this.agentList = agentList;
notifyDataSetChanged()
}
公共代理(列表代理列表){
this.agentList.clear()
this.agentList=agentList;
notifyDataSetChanged()
}
您必须在设置LayoutManager后设置AgentAdapter,然后在设置适配器的响应上刷新数据集
要刷新数据集,只需在AgentAdapter中创建一个小函数,如:
public void refreshAgents(List<Agent> agentList){
this.agentList.clear()
this.agentList = agentList;
notifyDataSetChanged()
}
公共代理(列表代理列表){
this.agentList.clear()
this.agentList=agentList;
notifyDataSetChanged()
}
E/RecyclerView:未连接适配器;,数据不希望出现在片段主页中
这是因为您没有将任何数据添加到片段中,而是添加到您的活动中
E/RecyclerView:未连接适配器;跳过布局
这是因为您从未将适配器连接到活动中的回收视图
。您需要先初始化适配器,然后将其连接到回收视图,如下所示:
List<Agent> agentList;
public RecyclerView recyclerView;
// declare the adapter so it can be re-accessed later.
private AgentAdapter mAgentAdapter;
@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<>();
// initialize the adapter
mAgentAdapter = new AgentAdapter(this, agentList);
// attach the adapter to the RecyclerView
recyclerView.setAdapter(mAgentAdapter);
...
}
列表代理列表;
公共回收视图回收视图;
//声明适配器,以便以后可以重新访问它。
私人代理适配品红色;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u代理);
//从xml获取recyclerview
recyclerView=findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//初始化产品列表
agentList=新的ArrayList();
//初始化适配器
洋红适配器=新代理适配器(此,代理列表);
//将适配器连接到RecyclerView
recyclerView.setAdapter(品红适配器);
...
}
然后,无论何时更改适配器的整个列表,都要告诉它刷新:
// sample of adding data from your backend.
for (int i = 0; i < array.length(); i++) {
JSONObject agent = array.getJSONObject(i);
agentList.add(new Agent(
agent.getInt("idagent"),
agent.getString("nomagent")
));
}
// update the adapter.
mAgentAdapter.notifyDataSetChanged();
//从后端添加数据的示例。
对于(int i=0;i
E/RecyclerView:未连接适配器;,数据不希望出现在片段主页中
这是因为您没有将任何数据添加到片段中,而是添加到您的活动中
E/RecyclerView:未连接适配器;跳过布局
这是因为您从未将适配器连接到活动中的回收视图
。您需要先初始化适配器,然后将其连接到回收视图,如下所示:
List<Agent> agentList;
public RecyclerView recyclerView;
// declare the adapter so it can be re-accessed later.
private AgentAdapter mAgentAdapter;
@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<>();
// initialize the adapter
mAgentAdapter = new AgentAdapter(this, agentList);
// attach the adapter to the RecyclerView
recyclerView.setAdapter(mAgentAdapter);
...
}
列表代理列表;
公共回收视图回收视图;
//声明适配器,以便以后可以重新访问它。
私人代理适配品红色;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u代理);
//从xml获取recyclerview
recyclerView=findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
//初始化产品列表
agentList=新的ArrayList();
//初始化适配器
品红适配器=新代理适配器(th