我有Create Recyclerview,而不是我想要从数据库中检索数据到aandroid recyclerview。我通过php通过php将mysqlserver中的数据检索到JSON formate中,但是当我尝试在recyclerview中检索时,数据未显示在recyclerview中。
ActivityMain.java :(下面给出其代码):
package com.example.chatapp;
import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
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 MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<PoliceStations> policeStations;
private static final String Police_station_URL="url of my php scrip";
Adapter adapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
policeStations=new ArrayList<>();
Load_Police_Stations();
}
private void Load_Police_Stations()
{
StringRequest stringRequest=new StringRequest(Request.Method.GET, Police_station_URL, new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
try
{
JSONArray jsonArray=new JSONArray(response);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
int id=jsonObject.getInt("id");
String policeStation=jsonObject.getString("Police_Station");
PoliceStations policeStations1=new PoliceStations(id,policeStation);
policeStations.add(policeStations1);
}
adapter=new Adapter(MainActivity.this,policeStations);
recyclerView.setAdapter(adapter);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
enter code here
PoliceStations.java(此活动用于设置值) 包com.example.chatapp;
public class PoliceStations
{
private int id;
private String policestations;
public PoliceStations(int id, String policestations)
{
this.id = id;
this.policestations = policestations;
}
public int getId()
{
return id;
}
public String getPolicestations()
{
return policestations;
}
}
enter code here
(Adapter.java) 此活动用于采用datasoure的值(在我的情况下为JSON数组文件):
package com.example.chatapp;
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 Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
{
private Context context;
List<PoliceStations> stations;
public Adapter(Context context, List<PoliceStations> stations)
{
this.context = context;
this.stations = stations;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
LayoutInflater inflater=LayoutInflater.from(context);
View view=inflater.inflate(R.layout.layout_for_police_stations,viewGroup,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i)
{
PoliceStations policeStations=stations.get(i);
viewHolder.police.setText(policeStations.getPolicestations());
}
@Override
public int getItemCount()
{
return stations.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView police;
public ViewHolder(@NonNull View itemView)
{
super(itemView);
police=itemView.findViewById(R.id.police);
}
}
}
enter code here
card.xml:
this is layout activity of my project that is used to design layout for individual item in recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView
android:layout_marginTop="20dp"
android:id="@+id/police"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
enter code here
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=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>