使用 volley 将 mysql 数据库中的数据添加到 tableLayout
adding data from mysql database to tableLayout using volley
我正在尝试使用 volley 将数据添加到我的 table布局,如下所示
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);
}
程序运行没有错误,但是尽管数据库中有正在获取的数据,但没有添加行,当我调试每个单独的 json对象值时,我得到了一些东西,即 Log.d(TAG, "onResponse: "+jsonObject.getString("monthlyrevenue"));
在控制台中给出 45000,但该行是空的。
我可能做错了什么或者我怎样才能设法将数据添加到 table?
这是我的 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}]}
查看您的 JSON 响应,倒数第三个对象名称是不带下划线的 regdate,但在您的 created.setText()
方法中,您使用带下划线的名称 reg_date 引用了它。更正将解决您的问题
我正在尝试使用 volley 将数据添加到我的 table布局,如下所示
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);
}
程序运行没有错误,但是尽管数据库中有正在获取的数据,但没有添加行,当我调试每个单独的 json对象值时,我得到了一些东西,即 Log.d(TAG, "onResponse: "+jsonObject.getString("monthlyrevenue"));
在控制台中给出 45000,但该行是空的。
我可能做错了什么或者我怎样才能设法将数据添加到 table?
这是我的 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}]}
查看您的 JSON 响应,倒数第三个对象名称是不带下划线的 regdate,但在您的 created.setText()
方法中,您使用带下划线的名称 reg_date 引用了它。更正将解决您的问题