我在哪里放置功能来显示加载过程?

时间:2013-10-19 04:16:17

标签: java android android-layout android-intent android-listview

我希望在我的应用程序从数据库加载数据时显示加载过程。

这是我的Java文件。

我必须在哪里放置显示加载过程的函数?

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);
        }
    });     
}
}

2 个答案:

答案 0 :(得分: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();
    }
}

有关异步任务及其方法的更多说明,请查看here

答案 1 :(得分:0)

您必须使用AsyncTask类,覆盖doInBackground方法来执行数据库提取,使用onPreExecute方法显示您的加载,并onPostExecute隐藏它。您可以参考AsynTask processAsynTask post了解更多信息