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

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

adding data from mysql database to tableLayout using volley

问题

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

private void getData() {
    final ArrayList<Detail> drinks = new ArrayList<>();
    final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(),
            20, 60, ContextCompat.getColor(getActivity(), R.color.red));
    loader.setAnimDuration(3000);
    content.addView(loader);
    StringRequest drinkStringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_DETAILS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jsonObject;
                    try {
                        JSONObject drinkObject = new JSONObject(response);
                        JSONArray drinkArray = drinkObject.getJSONArray("data");
                        for (int i = 0; i < drinkArray.length(); i++) {
                            jsonObject = drinkArray.getJSONObject(i);
                            tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.table_item, null, false);
                            //table components
                            id = tableRow.findViewById(R.id.id);
                            name = tableRow.findViewById(R.id.name);
                            ppn = tableRow.findViewById(R.id.ppn);
                            spn = tableRow.findViewById(R.id.spn);
                            email = tableRow.findViewById(R.id.email);
                            idno = tableRow.findViewById(R.id.idNo);
                            btype = tableRow.findViewById(R.id.btype);
                            loc = tableRow.findViewById(R.id.loc);
                            monthly = tableRow.findViewById(R.id.monthly);
                            status = tableRow.findViewById(R.id.status);
                            created = tableRow.findViewById(R.id.created);
                            id.setText(String.valueOf(jsonObject.getInt("id")));
                            name.setText(jsonObject.getString("name"));
                            ppn.setText(jsonObject.getString("primaryphone"));
                            spn.setText(jsonObject.getString("secondaryphone"));
                            email.setText(jsonObject.getString("email"));
                            idno.setText(String.valueOf(jsonObject.getInt("idno")));
                            btype.setText(jsonObject.getString("businesstype"));
                            loc.setText(jsonObject.getString("location"));
                            monthly.setText(jsonObject.getString("monthlyrevenue"));
                            created.setText(jsonObject.getString("reg_date"));
                            status.setText(String.valueOf(jsonObject.getInt("status")));
                            items.addView(tableRow);
                        }
                        content.removeView(loader);

                    } catch (JSONException e) {
                        content.removeView(loader);
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    content.removeView(loader);
                    error.printStackTrace();
                }
            });
    RequestQueue requestQue = Volley.newRequestQueue(getActivity());
    requestQue.add(drinkStringRequest);
}

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

这是我的JSON响应:

{"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

    private void getData() {
final ArrayList &lt;Detail&gt; drinks = new ArrayList&lt;&gt;();
final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(),
20, 60, ContextCompat.getColor(getActivity(), R.color.red));
loader.setAnimDuration(3000);
content.addView(loader);
StringRequest drinkStringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_DETAILS,
new Response.Listener&lt;String&gt;() {
@Override
public void onResponse(String response) {
JSONObject jsonObject;
try{
JSONObject drinkObject = new JSONObject(response);
JSONArray drinkArray = drinkObject.getJSONArray(&quot;data&quot;);
for(int i=0; i&lt;drinkArray.length();i++){
jsonObject = drinkArray.getJSONObject(i);
tableRow = LayoutInflater.from(getActivity()).inflate(R.layout.table_item, null, false);
//table components
id = tableRow.findViewById(R.id.id);
name = tableRow.findViewById(R.id.name);
ppn = tableRow.findViewById(R.id.ppn);
spn = tableRow.findViewById(R.id.spn);
email = tableRow.findViewById(R.id.email);
idno = tableRow.findViewById(R.id.idNo);
btype = tableRow.findViewById(R.id.btype);
loc = tableRow.findViewById(R.id.loc);
monthly = tableRow.findViewById(R.id.monthly);
status = tableRow.findViewById(R.id.status);
created = tableRow.findViewById(R.id.created);
id.setText(String.valueOf(jsonObject.getInt(&quot;id&quot;)));
name.setText(jsonObject.getString(&quot;name&quot;));
ppn.setText(jsonObject.getString(&quot;primaryphone&quot;));
spn.setText(jsonObject.getString(&quot;secondaryphone&quot;));
email.setText(jsonObject.getString(&quot;email&quot;));
idno.setText(String.valueOf(jsonObject.getInt(&quot;idno&quot;)));
btype.setText(jsonObject.getString(&quot;businesstype&quot;));
loc.setText(jsonObject.getString(&quot;location&quot;));
monthly.setText(jsonObject.getString(&quot;monthlyrevenue&quot;));
created.setText(jsonObject.getString(&quot;reg_date&quot;));
status.setText(String.valueOf(jsonObject.getInt(&quot;status&quot;)));
items.addView(tableRow);
}
content.removeView(loader);
}catch (JSONException e){
content.removeView(loader);
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
content.removeView(loader);
error.printStackTrace();
}
});
RequestQueue requestQue = Volley.newRequestQueue(getActivity());
requestQue.add(drinkStringRequest);
}

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

    {&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:

确定