Эй, я новичок в разборе данных из базы данных mysql с использованием json и volley, проблема в том, что данные просто хотят показать во фрагменте и показать только несколько секунд после того, как список исчез, как на этой картинке Проблема Проблема и данные списка не будут отображаться во фрагменте, который я хочу, чтобы он отображался на фрагментах, поэтому
Это код, который я использовал для показа на фрагментах
fragment.java
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();
}
}
}
так как же сделать так, чтобы данные можно было похвастаться на фрагментах? поэтому после хранения данных в базе данных из базы данных будут отображаться фрагменты
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Nama"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/nm2" />
<TextView
android:id="@+id/kmn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nm2"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="Komentar"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>