I am trying to get data from a JSON File using volley method and show it in RecyclerView but its not showing any data on RecyclerView. mysql Databse is configured on xampp. Code is below. I am new to android so you have to give your answers in detail.
Here is my PHP file getdata.php:
$connect = mysqli_connect("127.0.0.1", "root" , "", "recycleview_volley");
$query = "SELECT * FROM image_data";
$result = mysqli_query($connect,$query);
$recycler_data = array();
while($row = mysqli_fetch_array($result))
{
$recycler_data [] = array(
'image_url' => $row["image_url"],
'image_name' => $row["image_name"]
);
}
echo json_encode($recycler_data);
my MainActivity.java:
package com.example.razaz.recyclerviewpractice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
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.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
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.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RequestQueue mQueue;
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();
public String url = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started.");
mQueue = Volley.newRequestQueue(this);
getData();
initRecyclerView();
}
private void initRecyclerView(){
Log.d(TAG, "initRecyclerView: init recyclerView.");
RecyclerView recyclerView = findViewById(R.id.recycler_view);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, mNames, mImageUrls);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void getData(){
url = "http://127.0.0.1/recycler_api/getdata.php";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = response.getJSONObject(i);
mImageUrls.add(jsonObject.getString("image_url"));
mNames.add(jsonObject.getString("image_name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
error.getStackTrace();
}
});
mQueue.add(jsonArrayRequest);
}
}
my RecyclerViewAdapter.java
package com.example.razaz.recyclerviewpractice;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
ArrayList<String> mImagesNames = new ArrayList<>();
ArrayList<String> mImages = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context mContext, ArrayList<String> mImagesNames, ArrayList<String> mImages) {
this.mImagesNames = mImagesNames;
this.mImages = mImages;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_listitem, viewGroup, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder( ViewHolder viewHolder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(mImages.get(position))
.into(viewHolder.image) ;
viewHolder.imageName.setText(mImagesNames.get(position));
viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: Clicked on: "+ mImages.get(position));
Toast.makeText(mContext, mImagesNames.get(position), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mImagesNames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
CircleImageView image;
TextView imageName;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
imageName = itemView.findViewById(R.id.image_name);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
I think there is a bug in volley.
答案 0 :(得分:2)
在Adapter类中编写:
private ArrayList<String> mImageList;
public void setDataChange(ArrayList<String> imageList) {
mImageList = new ArrayList<>(imageList);
notifyDataSetChanged();
}
在onResponse()
内部的Volley响应方法上获得成功结果后,调用此方法。
根据您的List<String>
解析结果,创建一个列表并将其添加到全局列表中并调用:
adapter.setDataChange(imageList);
或者另一种方式:
创建imageUrl和imageName的pojo;
private List<PojoClass> imageList;
在onResponse()中获得结果后, 这样做:
imageList =new List<PojoClass>;
PojoClass pojo=new PojoClass();
pojo.setImageUrl(imageUrl);
pojo.setImageName(imageName);
imageList.add(pojo);
像这样通过适配器传递它:
adapter.setDataChange(imageList);
答案 1 :(得分:2)
在侦听器中: 将接口或侦听器设置为:
public interface OnRowItemClick {
void onClick(int position, PojoClass pojo);
}
在Adpter类中:
声明点击监听器对象:
private OnRowItemClick mOnRowItemClick;
private List<PojoClass> imageList;
在适配器的构造函数中获取对象:
public HomeCategoriesAdapter(Context mContext, OnRowItemClick mOnRowItemClick,
ArrayList<PojoClass> imageArrayList) {
this.mContext = mContext;
this.mOnItemClickListener = mOnItemClickListener;
this.imageList = imageArrayList;
}
在bindView方法中,您可以在其中设置详细信息:
PojoClass pojo = imageList.get(position);
viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onClick(position, pojo);
}
});
在MainActivity中:
声明您的适配器类:
private List<PojoClass> imageList;
private MyItemAdapter myItemAdapter;
初始化您的适配器类:
List<YourModel> modelItemList;
modelItemList = new ArrayList<>();
myItemAdapter = new MyItemAdapter(mContext, this, modelItemList);
recyclerView.setAdapter(myItemAdapter);
服务后:
imageList =new List<PojoClass>;
PojoClass pojo=new PojoClass();
pojo.setImageUrl(imageUrl);
pojo.setImageName(imageName);
imageList.add(pojo);
adapter.setDataChange(imageList);