[:en]Android: AsyncTask, ListView,Toast & ProgressBar (Simpliest)[:]

[:en]AsyncTaskToast

Step 1: activity_main.xml


    

    

    





Step 2: MainActivity.java


package com.example.cambridge.asynctasklistview;

import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {

    ListView mainlist;
    private String[] texts = {"Honda","Yamaha","Apple","Honda","Yamaha","Apple","Honda","Yamaha","Apple","Honda","Yamaha","Apple"};



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add ProgressBar. Must be before setContentView: Doesn't work
        //requestWindowFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.activity_main);
        // Initialize listview
        mainlist = (ListView)findViewById(R.id.listView1);
        // Set Adapter & pass empty arraylist
        mainlist.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,new ArrayList()));
        // Start MyTask
        new MyTask().execute();

    }

    class MyTask extends AsyncTask{

        ArrayAdapter adapter;
        ProgressBar progressBar;
        int count;

        @Override
        protected void onPreExecute() {
            // Setup adapter
            adapter= (ArrayAdapter) mainlist.getAdapter();
            progressBar = (ProgressBar)findViewById(R.id.progress_bar);
            // Set parameters
            progressBar.setMax(9);
            progressBar.setProgress(0);
            progressBar.setVisibility(View.VISIBLE);
            count = 0;

        }

        @Override
        protected Void doInBackground(Void... params) {
            for(String item: texts){
                // pass single item
                publishProgress(item);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(String... values) {
            // add the first item
            adapter.add(values[0]);
            count++;
            progressBar.setProgress(count);

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressBar.setVisibility(View.GONE);
            L.s(MainActivity.this,"All items were added successfully!");
        }
    }
}

Step 3: Toast

package com.example.cambridge.asynctasklistview;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by cambridge on 14/2/16.
 */
public class L {
    public static void m(String message){

        Log.d("VIVZ", message);
    }
    public static void s(Context context,String message){
        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();

    }

}

[:]

Print Friendly, PDF & Email
Scroll to Top