如何在Recyclerview中显示json数据?

时间:2016-10-30 17:27:07

标签: 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);
}
  

适配器

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);
    }
}}
  

列表项

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;
}}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

之后

rewayaList.add(new listitem_gib(id, name, img, url, num, size));

添加

adapter.notifyDataSetChanged();

有关详细说明,请访问Official Documentation for the function notifyDataSetChanged。这只是几句话,所以不要害怕它可能是一个冗长的文档。