android - 如何在Recyclerview中显示json数据?

标签 android mysql json

我想在Recyclerview中显示来自Mysql数据库的数据,我使用这个代码,但是我得到白页,没有任何显示,我不知道错误在哪里,请帮助我找到错误,请解释你的答案,因为我是android新手,这是我的代码

Mainactivity.java

RequestQueue requestQueue;
ArrayList<listitem_gib> rewayaList = new ArrayList<>();

private RecyclerView recyclerView;

ProgressBar progressBar;
LinearLayout progress_layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardview);

    TextView progress_txt = (TextView) findViewById(R.id.progress_txt);
    progress_layout = (LinearLayout) findViewById(R.id.progress_layout);
    progressBar = (ProgressBar) findViewById(R.id.progress_bar);

    String url = "http://grassyhat.com/android/arabic.php";

    requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {
                public void onResponse(JSONObject response) {

                    progress_layout.setVisibility(View.GONE);

                    try {
                        JSONArray jsonArray = response.getJSONArray("all");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject respons = jsonArray.getJSONObject(i);
                            String id = respons.getString("id");
                            String name = respons.getString("name");
                            String img = respons.getString("img");
                            String url = respons.getString("url");
                            String num = respons.getString("num");
                            String size = respons.getString("size");
                            rewayaList.add(new listitem_gib(id, name, img, url, num, size));

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("VOLLEY", "ERROR");
        }
    }

    );
    requestQueue.add(jsonObjectRequest);


    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);

    //Just your list of objects, in your case the list that comes from the db
    CardAdapter adapter = new CardAdapter(rewayaList, this);

    //RecyclerView needs a layout manager in order to display data so here we create one
    StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);

    //Here we set the layout manager and the adapter to the listview
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}

Adapter

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.CardViewHolder> {

private List<listitem_gib> rewayaList;


Context context;


public CardAdapter(List<listitem_gib> rewayaList, Context context) {

    super();

    this.rewayaList = rewayaList;
    this.context = context;
}

@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);

    CardViewHolder viewHolder = new CardViewHolder(v);

    return viewHolder;
}

@Override
public void onBindViewHolder(CardViewHolder holder, int position) {
    //Here you bind your views with the data from each object from the list

    listitem_gib rewaya = rewayaList.get(position);
    holder.rewaya_name.setText(rewaya.name);
    holder.writer_name.setText(rewaya.num);


}

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

public class CardViewHolder extends RecyclerView.ViewHolder {

    public ImageView Image;
    public TextView rewaya_name, writer_name;

    public CardViewHolder(View itemView) {
        super(itemView);
        Image = (ImageView) itemView.findViewById(R.id.image);
        rewaya_name = (TextView) itemView.findViewById(R.id.Main_Text);
        writer_name = (TextView) itemView.findViewById(R.id.Second_Text);
    }
}}

ListItem

public class listitem_gib {

public String id;
public String name;
public String img;
public String url;
public String num;
public String size;

public listitem_gib(String id, String name, String img, String url, String num, String size) {
    this.id = id;
    this.name = name;
    this.img = img;
    this.url = url;
    this.num = num;
    this.size = size;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

public String getSize() {
    return size;
}

public void setSize(String size) {
    this.size = size;
}}

最佳答案

很难通过代码找出错误 在这种情况下,您必须分解代码并单独测试每个部分 首先通过 LOG 或 toast 打印响应来检查响应是否正确来自服务器,然后在有 arrayLst 时检查解析部分 然后将数据填充到回收器 View

关于android - 如何在Recyclerview中显示json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40331672/

相关文章:

javascript - 如何在Javascript中对Json数据进行排序?

json - PhoneGap应用程序错误白名单拒绝: url ='http://xyz.com'

java - 打开自定义相机,拍照,保存图片,发送图片

android - 如何覆盖 RecyclerView 适配器的方法 notifyDataSetChanged、notifyItemChanged ...

android - 在安卓中打开谷歌地图

mysql 将日期打印为日期(周数)

jquery - 来源http ://localhost is not allowed by Access-Control-Allow-Origin.?

android - 丢失 keystore Android

mysql - 如何使用 MySql 使用一对多关系获取数据并在 MySql 中进行联接

php - 如何将 MySQL 表记录分成两半以显示在页面的每一侧