-1

当我的应用程序从数据库加载数据时,我想显示加载过程。

这是我的 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);
        }
    });     
}
}
4

2 回答 2

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

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

于 2013-10-19T04:23:47.437 回答
0

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

于 2013-10-19T04:22:17.370 回答