[:en]Android: Toast class, Progress Dialog & AsyncTask with http with ListView[:]

[:en]Toast: can be called from anywhere

activity_main.xml

Toast: L.java


package com.inventlinks.estaff;


import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class makeup extends AppCompatActivity {

        private String[] names={"John","Simon","Amy","Rose","Joe","Cruise","Tim","Jim","Pete"};

        ListView listView;
        private ProgressDialog dialog;
        String method = "cw";
        String dbname = "cw";
        String login_name = "513";




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_makeup);
            // Setup Dialog
            dialog = new ProgressDialog(this);
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);
            dialog.setMessage("Please wait...");

            listView = (ListView)findViewById(R.id.makeuplv);
            listView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,new ArrayList()));
            // Call AsyncTask
           new MyTask().execute(method,dbname,login_name);
        }

        class MyTask extends AsyncTask {

            //Define variables
            ArrayAdapter adapter;
            //AlertDialog alertDialog;
            // ProgressDialog dialog;

            @Override
            protected void onPreExecute() {
                adapter = (ArrayAdapter)listView.getAdapter();
               // dialog = ProgressDialog.show(makeup.this, "Please Wait...",null, true, true);
                dialog.show();
               // Toast.makeText(makeup.this, dbname, Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {

                String makeup_url = "http://xxx.php";
                String method = params[0];
                String dbnamechosen = params[1].trim();
                String userchosen = params[2].trim();
                try {
                    URL url = new URL(makeup_url);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
                    String data = URLEncoder.encode("dbname","UTF-8")+"="+URLEncoder.encode(dbnamechosen,"UTF-8")+"&"+
                            URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(userchosen,"UTF-8");
                    bufferedWriter.write(data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                    String response = "";
                    String line = "";
                    while ((line = bufferedReader.readLine())!=null)
                    {
                        response+= line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return response;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                 return null;
            }
           /*
            @Override
            protected void onProgressUpdate(String... values) {
                // add names to the adapter
                adapter.add(values[0]);

            }*/

            @Override
            protected void onPostExecute(String result) {
                dialog.dismiss();
                //Toast.makeText(makeup.this, result, Toast.LENGTH_LONG).show();
                String[] arrayList = result.trim().split(",");
                listView = (ListView)findViewById(R.id.makeuplv);
                listView.setAdapter(new ArrayAdapter(makeup.this, android.R.layout.simple_list_item_1, arrayList));
                // Keep updating any changes
                adapter.notifyDataSetChanged();
            }
        }
    }

[:]

Print Friendly, PDF & Email
Scroll to Top