将数据从MySQL数据库添加到TableLayout中,使用Volley。

huangapple go评论138阅读模式
英文:

adding data from mysql database to tableLayout using volley

问题

我正尝试使用Volley将数据添加到我的tableLayout,如下所示:

  1. private void getData() {
  2. final ArrayList<Detail> drinks = new ArrayList<>();
  3. final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(),
  4. 20, 60, ContextCompat.getColor(getActivity(), R.color.red));
  5. loader.setAnimDuration(3000);
  6. content.addView(loader);
  7. StringRequest drinkStringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_DETAILS,
  8. new Response.Listener<String>() {
  9. @Override
  10. public void onResponse(String response) {
  11. JSONObject jsonObject;
  12. try {
  13. JSONObject drinkObject = new JSONObject(response);
  14. JSONArray drinkArray = drinkObject.getJSONArray("data");
  15. for (int i = 0; i < drinkArray.length(); i++) {
  16. jsonObject = drinkArray.getJSONObject(i);
  17. tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.table_item, null, false);
  18. //table components
  19. id = tableRow.findViewById(R.id.id);
  20. name = tableRow.findViewById(R.id.name);
  21. ppn = tableRow.findViewById(R.id.ppn);
  22. spn = tableRow.findViewById(R.id.spn);
  23. email = tableRow.findViewById(R.id.email);
  24. idno = tableRow.findViewById(R.id.idNo);
  25. btype = tableRow.findViewById(R.id.btype);
  26. loc = tableRow.findViewById(R.id.loc);
  27. monthly = tableRow.findViewById(R.id.monthly);
  28. status = tableRow.findViewById(R.id.status);
  29. created = tableRow.findViewById(R.id.created);
  30. id.setText(String.valueOf(jsonObject.getInt("id")));
  31. name.setText(jsonObject.getString("name"));
  32. ppn.setText(jsonObject.getString("primaryphone"));
  33. spn.setText(jsonObject.getString("secondaryphone"));
  34. email.setText(jsonObject.getString("email"));
  35. idno.setText(String.valueOf(jsonObject.getInt("idno")));
  36. btype.setText(jsonObject.getString("businesstype"));
  37. loc.setText(jsonObject.getString("location"));
  38. monthly.setText(jsonObject.getString("monthlyrevenue"));
  39. created.setText(jsonObject.getString("reg_date"));
  40. status.setText(String.valueOf(jsonObject.getInt("status")));
  41. items.addView(tableRow);
  42. }
  43. content.removeView(loader);
  44. } catch (JSONException e) {
  45. content.removeView(loader);
  46. e.printStackTrace();
  47. }
  48. }
  49. },
  50. new Response.ErrorListener() {
  51. @Override
  52. public void onErrorResponse(VolleyError error) {
  53. content.removeView(loader);
  54. error.printStackTrace();
  55. }
  56. });
  57. RequestQueue requestQue = Volley.newRequestQueue(getActivity());
  58. requestQue.add(drinkStringRequest);
  59. }

该程序没有错误运行,但尽管数据库中有数据被获取,但行未被添加。当我逐个调试每个单独的jsonObject值时,例如 Log.d(TAG, "onResponse: "+jsonObject.getString("monthlyrevenue"));,控制台会输出45000,但行为空。
我可能做错了什么,或者我如何能够将数据添加到表格中?

这是我的JSON响应:

  1. {"data":[{"id":1,"name":"test","primaryphone":"0712345678","secondaryphone":"0723456789","email":"test@test.com","idno":123456789,"businesstype":"test","location":"test","monthlyrevenue":"45000","regdate":"2020-09-21 11:20:05","updatedate":null,"status":1}]}
英文:

I am trying to add data to my tableLayout using volley as follows

  1. private void getData() {
  2. final ArrayList &lt;Detail&gt; drinks = new ArrayList&lt;&gt;();
  3. final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(),
  4. 20, 60, ContextCompat.getColor(getActivity(), R.color.red));
  5. loader.setAnimDuration(3000);
  6. content.addView(loader);
  7. StringRequest drinkStringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_DETAILS,
  8. new Response.Listener&lt;String&gt;() {
  9. @Override
  10. public void onResponse(String response) {
  11. JSONObject jsonObject;
  12. try{
  13. JSONObject drinkObject = new JSONObject(response);
  14. JSONArray drinkArray = drinkObject.getJSONArray(&quot;data&quot;);
  15. for(int i=0; i&lt;drinkArray.length();i++){
  16. jsonObject = drinkArray.getJSONObject(i);
  17. tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.table_item, null, false);
  18. //table components
  19. id = tableRow.findViewById(R.id.id);
  20. name = tableRow.findViewById(R.id.name);
  21. ppn = tableRow.findViewById(R.id.ppn);
  22. spn = tableRow.findViewById(R.id.spn);
  23. email = tableRow.findViewById(R.id.email);
  24. idno = tableRow.findViewById(R.id.idNo);
  25. btype = tableRow.findViewById(R.id.btype);
  26. loc = tableRow.findViewById(R.id.loc);
  27. monthly = tableRow.findViewById(R.id.monthly);
  28. status = tableRow.findViewById(R.id.status);
  29. created = tableRow.findViewById(R.id.created);
  30. id.setText(String.valueOf(jsonObject.getInt(&quot;id&quot;)));
  31. name.setText(jsonObject.getString(&quot;name&quot;));
  32. ppn.setText(jsonObject.getString(&quot;primaryphone&quot;));
  33. spn.setText(jsonObject.getString(&quot;secondaryphone&quot;));
  34. email.setText(jsonObject.getString(&quot;email&quot;));
  35. idno.setText(String.valueOf(jsonObject.getInt(&quot;idno&quot;)));
  36. btype.setText(jsonObject.getString(&quot;businesstype&quot;));
  37. loc.setText(jsonObject.getString(&quot;location&quot;));
  38. monthly.setText(jsonObject.getString(&quot;monthlyrevenue&quot;));
  39. created.setText(jsonObject.getString(&quot;reg_date&quot;));
  40. status.setText(String.valueOf(jsonObject.getInt(&quot;status&quot;)));
  41. items.addView(tableRow);
  42. }
  43. content.removeView(loader);
  44. }catch (JSONException e){
  45. content.removeView(loader);
  46. e.printStackTrace();
  47. }
  48. }
  49. },
  50. new Response.ErrorListener() {
  51. @Override
  52. public void onErrorResponse(VolleyError error) {
  53. content.removeView(loader);
  54. error.printStackTrace();
  55. }
  56. });
  57. RequestQueue requestQue = Volley.newRequestQueue(getActivity());
  58. requestQue.add(drinkStringRequest);
  59. }

The program runs with no errors but the rows are not added despite there being data in the database that is being fetched and when I debug each individual jsonObject value I get something i.e Log.d(TAG, &quot;onResponse: &quot;+jsonObject.getString(&quot;monthlyrevenue&quot;)); gives 45000 in the console but the row is empty.
What could I be dooing wrong or rather how can I manage to add the data to the table?

This is my json response

  1. {&quot;data&quot;:[{&quot;id&quot;:1,&quot;name&quot;:&quot;test&quot;,&quot;primaryphone&quot;:&quot;0712345678&quot;,&quot;secondaryphone&quot;:&quot;0723456789&quot;,&quot;email&quot;:&quot;test@test.com&quot;,&quot;idno&quot;:123456789,&quot;businesstype&quot;:&quot;test&quot;,&quot;location&quot;:&quot;test&quot;,&quot;monthlyrevenue&quot;:&quot;45000&quot;,&quot;regdate&quot;:&quot;2020-09-21 11:20:05&quot;,&quot;updatedate&quot;:null,&quot;status&quot;:1}]}

答案1

得分: 0

查看您的JSON响应,倒数第三个对象的名称是"regdate",没有下划线,但在您的created.setText()方法中,您使用了带下划线的名称"reg_date"来引用它。纠正这一点将解决您的问题。

英文:

Looking at your JSON response the third last object name is regdate without underscore but in your created.setText() method, you have referred to it using the name reg_date with underscore. Correcting that will solve your problem

huangapple
  • 本文由 发表于 2020年9月21日 20:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63992173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定