1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
package com.monapp.ui.gallery;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import android.content.Context;
import android.os.AsyncTask;
import com.monapp.R;
public class GalleryFragment extends Fragment {
static final String url = "jdbc:mysql://MON.IP:3306/tipe-ag_principale";
static final String user = "mon_identifiant";
static final String pass = "mon_mot_de_passe";
public static List<Float> objList;
private GalleryViewModel galleryViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
final ListView listeView= root.findViewById(R.id.listView);
new Download(getContext(), url, user, pass);
final ArrayAdapter<Float> adapter = new ArrayAdapter<Float>(getContext(),android.R.layout.simple_list_item_1, objList);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>()
{
@Override
public void onChanged(@Nullable String s) {
textView.setText("test");
listeView.setAdapter(adapter);
//android.R.layout.simple_list_item_1 est une vue disponible de base dans le SDK android,
//Contenant une TextView avec comme identifiant "@android:id/text1"
}
});
return root;
}
public class Download extends AsyncTask<Void, Void, String> {
ProgressDialog mProgressDialog;
Context context;
private String url;
private String user;
private String pass;
public Download(Context context, String url, String user, String pass) {
this.context = context;
this.url = url;
this.user = user;
this.pass = pass;
}
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(context, "",
"Please wait, getting database...");
}
protected String doInBackground(Void... params) {
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection(url, user, pass);
java.sql.Statement st = con.createStatement();
java.sql.ResultSet rs = st.executeQuery("Produits");
ArrayList<Float> list = new ArrayList<Float>();
while (rs.next()) {
Float field= rs.getFloat(2);
GalleryFragment.objList.add(new Float (field));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return "Complete";
}
protected void onPostExecute(String result) {
if (result.equals("Complete")) {
mProgressDialog.dismiss();
}
}
}} |
Partager