-1

I want to display a loading process when my application is loading data from the database.

This is my Java file.

Where do I have to put the function to display the loading process?

public class AksesServerActivity extends ListActivity {
private static String link_url = "http://plnskh.zz.mu/android/berita/cekdaftar.php";    
private static final String AR_ID = "id";
private static final String AR_JUDUL = "judul";
private static final String AR_CONTENT = "content";
JSONArray artikel = null;
ArrayList<HashMap<String, String>> daftar_artikel = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.AmbilJson(link_url);
    try {
        artikel = json.getJSONArray("artikel");         
        for(int i = 0; i < artikel.length(); i++){
            JSONObject ar = artikel.getJSONObject(i);           
            String id = ar.getString(AR_ID);
            String judul = ar.getString(AR_JUDUL);
            String content = ar.getString(AR_CONTENT).substring(0,100)+"...(baca selengkapnya)";                
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(AR_ID, id);
            map.put(AR_JUDUL, judul);
            map.put(AR_CONTENT, content);
            daftar_artikel.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    this.adapter_listview();
}

public void adapter_listview() {
    ListAdapter adapter = new SimpleAdapter(this, daftar_artikel,
            R.layout.list_item,
            new String[] { AR_JUDUL, AR_CONTENT, AR_ID}, new int[] {
                    R.id.judul, R.id.content, R.id.kode});
    setListAdapter(adapter);
    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            String kode = ((TextView) view.findViewById(R.id.kode)).getText().toString();               
            Intent in = new Intent(AksesServerActivity.this, DetailAksesServer.class);
            in.putExtra(AR_ID, kode);
            startActivity(in);
        }
    });     
}
}
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179

2 Answers2

1
public class LongOperation extends AsyncTask<String, String, String>
{
    ProgressDialog pdialog;
    @Override
    protected String doInBackground(String... params) {

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.AmbilJson(link_url);
        try {
            artikel = json.getJSONArray("artikel");         
            for(int i = 0; i < artikel.length(); i++){
                JSONObject ar = artikel.getJSONObject(i);           
                String id = ar.getString(AR_ID);
                String judul = ar.getString(AR_JUDUL);
                String content = ar.getString(AR_CONTENT).substring(0,100)+"...(baca selengkapnya)";                
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(AR_ID, id);
                map.put(AR_JUDUL, judul);
                map.put(AR_CONTENT, content);
                daftar_artikel.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pdialog.dismiss();
        this.adapter_listview();
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        pdialog = new ProgressDialog(NewsActivity.this);
        pdialog.setMessage("Loading");
        pdialog.show();
    }
}

for more clarification about asynchronous task and its methods, check here

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

You must use AsyncTask class, override doInBackground method to perform your databse fetch, onPreExecute method to show your loading and onPostExecute to hide it. you can refer AsynTask process and AsynTask post for more information

Community
  • 1
  • 1
Jans
  • 11,064
  • 3
  • 37
  • 45