Java 使用volley将mysql数据库中的数据添加到tableLayout
我正试图使用截击将数据添加到我的tableLayout中,如下所示Java 使用volley将mysql数据库中的数据添加到tableLayout,java,android,json,android-volley,android-tablelayout,Java,Android,Json,Android Volley,Android Tablelayout,我正试图使用截击将数据添加到我的tableLayout中,如下所示 private void getData() { final ArrayList <Detail> drinks = new ArrayList<>(); final RotatingCircularDotsLoader loader = new RotatingCircularDotsLoader(getActivity(), 20, 60, Contex
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响应,最后三个对象名是不带下划线的regdate,但在
created.setText()
方法中,您使用带有下划线的名称regu-date引用了它。纠正这一点将解决您的问题什么是项
?如果您确认数据解析正在工作(通过日志记录),那么发布布局XML和容器/活动
s布局会更合适
{"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}]}