嘿,我是初学者,使用json和volley解析mysql数据库中的数据,问题是数据只想显示在片段上,而在列表消失后仅显示几秒钟,就像这张图片Trouble Problem并且列表数据不会在片段中显示,我希望它在片段上显示
这是我用于片段显示的代码
fragment.java
sequelize.query()
那么如何使数据可以在片段上显示呢?因此从数据库数据存储到数据库中的数据将显示在碎片上
fragment.xml
public class KomentarFragment extends Fragment {
TextView nama,komen;
View v;
ArrayList<Komen> kmn = new ArrayList<>();
public static String url ="http://surveyclickon.000webhostapp.com/android_register_login/komen.php";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View kom = inflater.inflate(R.layout.fragment_komentar,container,false);
nama = kom.findViewById(R.id.nm2);
komen = kom.findViewById(R.id.kmn2);
new AsyncFetch().execute();
return kom;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private class AsyncFetch extends AsyncTask<String, String, String> {
ProgressDialog pDialog = new ProgressDialog(getActivity());
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Loading list ...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... strings) {
try {
// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL("http://surveyclickon.000webhostapp.com/android_register_login/back.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
try{
JSONObject object = new JSONObject(result);
int code = object.getInt("refund");
if(code == 0) {
JSONArray jsonArray = object.getJSONArray("refund");
ArrayList<Refund> ref = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Refund refund = new Refund();
refund.setNm(jsonObject.getString("nama"));
refund.setNo(jsonObject.getString("no"));
refund.setAlam(jsonObject.getString("alamat"));
refund.setKode(jsonObject.getString("kode"));
refund.setAlas(jsonObject.getString("alasan"));
ref.add(refund);
}
}
}catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
您基本上没有更新UI,所以一无所获
public class KomentarFragment extends Fragment {
List<Refund> refundList = new ArrayList<>();
....
}
然后在您的postExecute方法中
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Refund refund = new Refund();
refund.setNm(jsonObject.getString("nama"));
refund.setNo(jsonObject.getString("no"));
refund.setAlam(jsonObject.getString("alamat"));
refund.setKode(jsonObject.getString("kode"));
refund.setAlas(jsonObject.getString("alasan"));
refundList.add(refund);
}
//Update the UI
//Store to the database
然后,您可以根据需要使用recyclerview或数据库更新UI,也可以在postExecute方法中进行