Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TampilUser extends AppCompatActivity {
- ListView listView;
- CustomAdapter customAdapter;
- ArrayList<User> user = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tampil_user);
- listView = (ListView) findViewById(R.id.listUser);
- getJSON("http://www.trigroupindonesia.com/php/read_user.php");
- }
- @Override
- public void onResume() {
- super.onResume();
- getJSON("http://www.trigroupindonesia.com/php/read_user.php");
- }
- private void getJSON(final String urlWebService) {
- class GetJSON extends AsyncTask<Void, Void, String> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
- try {
- loadIntoListView(s);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected String doInBackground(Void... voids) {
- try {
- URL url = new URL(urlWebService);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- StringBuilder sb = new StringBuilder();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String json;
- sb.setLength(0);
- while ((json = bufferedReader.readLine()) != null) {
- sb.append(json + "n");
- }
- bufferedReader.close();
- con.disconnect();
- return sb.toString().trim();
- } catch (Exception e) {
- return null;
- }
- }
- }
- GetJSON getJSON = new GetJSON();
- getJSON.execute();
- }
- public void loadIntoListView(String json) throws JSONException {
- JSONArray jsonArray = new JSONArray(json);
- user = new ArrayList<>();
- user.clear();
- User u;
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject obj = jsonArray.getJSONObject(i);
- String email = obj.getString("email");
- String nama = obj.getString("nama");
- u = new User();
- u.setEmail(email);
- u.setNama(nama);
- user.add(u);
- }
- customAdapter = new CustomAdapter(getApplicationContext(),user);
- listView.setAdapter(customAdapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- String nama = customAdapter.user.get(position).getNama();
- String email = customAdapter.user.get(position).getEmail();
- Intent i = new Intent(getApplicationContext(), DetailUser.class);
- i.putExtra("email", email);
- i.putExtra("nama", nama);
- startActivityForResult(i, 0);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
- getJSON("http://www.trigroupindonesia.com/php/read_user.php");
- customAdapter.notifyDataSetChanged();
- customAdapter.notifyDataSetInvalidated();
- }
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- if(convertView==null)
- {
- convertView=inflater.inflate(R.layout.daftar_user,parent,false);
- }
- TextView textView_email = (TextView) convertView.findViewById(R.id.textView_email);
- TextView textView_nama = (TextView) convertView.findViewById(R.id.textView_nama);
- textView_email.setText(user.get(position).getEmail());
- textView_nama.setText(user.get(position).getNama());
- return convertView;
- }
Add Comment
Please, Sign In to add comment