英文:
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 <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);
}
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, "onResponse: "+jsonObject.getString("monthlyrevenue"));
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
{"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}]}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论