【问题标题】:How to display json data in Recyclerview?如何在 Recyclerview 中显示 json 数据?
【发布时间】:2016-10-30 17:27:06
【问题描述】:

我想在 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;
}}

【问题讨论】:

    标签: android mysql json


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      之后

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

      添加

      adapter.notifyDataSetChanged();
      

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

      【讨论】:

        猜你喜欢
        • 2019-01-09
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-20
        • 2015-04-03
        相关资源
        最近更新 更多