2013-03-01 247 views
2

我是Android新手。我設法解析JSON文件到我的應用程序。現在我想使用AsyncTask來獲取Spinning progressBa直到應用程序啓動並加載數據。我試圖閱讀很多東西,但他們只給出瞭如何獲取onclick事件或下載事件的進度條。如何在應用程序啓動時獲取Spinning Progress Bar

這是我的代碼...

我只是困惑如何在自己的應用程序的開機啓動進度和下面的代碼的一部分進入到asycTask類的方法....

package com.demojson; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.ListActivity; 


public class MainActivity extends ListActivity { 

    private static String url = "http://timepasssms.com/webservices/ws.php?type=category"; 

    private static final String TAG_DATA = "DATA"; 

    private static final String TAG_0 = "0"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_1 = "1"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_2 = "2"; 
    private static final String TAG_DESCRIPTION = "description"; 
    private static final String TAG_3 = "3"; 
    private static final String TAG_NEW_NAME = "new_name"; 
    private static final String TAG_4 = "4"; 
    private static final String TAG_STATUS = "status"; 

    JSONArray jArray = null; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     ArrayList<HashMap<String, String>> contents = new ArrayList<HashMap<String, String>>(); 

     JSONMethod metObj = new JSONMethod(); 
     JSONObject jOb = metObj.getUrl(url); 
     try { 
      jArray = jOb.getJSONArray(TAG_DATA); 

      for (int i = 0; i < jArray.length(); i++) { 
       JSONObject child = jArray.getJSONObject(i); 

       String tag_0 = child.getString(TAG_0); 
       String id = child.getString(TAG_ID); 
       String tag_1 = child.getString(TAG_1); 
       String name = child.getString(TAG_NAME); 
       String tag_2 = child.getString(TAG_2); 
       String description = child.getString(TAG_DESCRIPTION); 
       String tag_3 = child.getString(TAG_3); 
       String new_name = child.getString(TAG_NEW_NAME); 
       String tag_4 = child.getString(TAG_4); 
       String status = child.getString(TAG_STATUS); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(TAG_NAME , name); 

       contents.add(map); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     ListAdapter adapter = new SimpleAdapter(this, contents, 
       R.layout.list_items, new String[] { TAG_NAME }, 
       new int[] { R.id.tvName }); 
     setListAdapter(adapter); 

     // --To get listview set...-- 

     ListView lv = getListView(); 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       String item = ((TextView)view.findViewById(R.id.tvName)).getText().toString(); 
       Toast toast = Toast.makeText(getApplicationContext(), item , Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.CENTER, 0, 0); 
       toast.show(); 
      } 
     }); 
    } 


} 

和JSONMethod類...

package com.demojson; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONObject; 

import android.util.Log; 

public class JSONMethod { 
    InputStream is = null; 
    JSONObject jObj=null; 
    String json = ""; 



    public JSONObject getUrl(String url){ 
     try{ 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      HttpResponse httpRespone = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpRespone.getEntity(); 
      is = httpEntity.getContent(); 

     }catch(UnsupportedEncodingException ue){ 

     }catch(ClientProtocolException ce){ 

     }catch(IOException ie){ 

     } 

     try{ 
      BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 
      String line=null; 
      while((line=br.readLine())!= null){ 
       sb.append(line + "/n"); 
      } 
      is.close(); 
      json = sb.toString(); 

     }catch(Exception e){ 
      Log.e("JSONMethod", "Error converting result" + e.toString()); 
     } 
     try{ 
      jObj = new JSONObject(json); 
     }catch(Exception e){ 
      Log.e("JSONMethod", "Error parsing data" + e.toString()); 
     } 

     return jObj; 
    } 
} 

這裏XML文件... activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <!-- Main ListView 
     Always give id value as list(@android:id/list) 
    --> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="60dp" 
     android:src="@drawable/timepasssms" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

和list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/round_edge" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvName" 
     android:layout_width="fill_parent" 
     android:layout_height="45dp" 
     android:layout_marginLeft="10dp" 
     android:gravity="left" 
     android:padding="5dp" 
     android:textColor="#DF7401" 
     android:textSize="20dp" /> 

</LinearLayout> 
+0

,你可以簡單地使用ProgressDialog中的onExExcute()和onPostExecute方法的asynctask..just在preExecute方法中啓動它,並在PostExecute方法中關閉它... – 2013-03-01 08:23:58

回答

0

嘗試使用異步類,並在doInBackground()方法加載您的列表視圖。

這是如何調用異步類

new LoadListView(this).execute(listView); 

,這裏是你的異步類

public class LoadListView extends AsyncTask<ListView> { 


private Context context; 
public loadListView(Context context) 
{ 
    this.context = context; 


} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    mProgressDialog.show(); 
} 

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

    //Here you need to load your ListView 
    return null; 

} 

protected void onProgressUpdate(String... progress) { 

    //Show progress bar here 
} 

@Override 
protected void onPostExecute(String unused) { 
    //Hide Progress Bar Here 
} 

加載列表視圖時,它會自動隱藏進度條..

+0

它與小修改工作...謝謝... – Siddharth 2013-03-01 11:06:00

+0

請標記答案爲接受。 – 2013-03-01 11:42:41

1

我這裏是'

@Override 
    protected void onPostExecute(Object table) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(table); 
     dialog.dismiss(); 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     dialog=ProgressDialog.show(Yourclass.this, "Loading Data.", "Please Wait"); 
    } 

` 我希望會有幫助

+0

它的幫助..但我想知道t帽子我怎麼能決定我的應用程序已加載listView,現在我應該調用onPostExecute方法? – Siddharth 2013-03-01 08:35:58

+0

我是如何做到這一點的,我創建了一個不可見按鈕,並在列表視圖填充後使用該按鈕的performClick()。在按鈕單擊我填充旋轉器..這將只在列表視圖填充後調用 – 2013-03-01 09:19:27

+0

我試過了..但沒有工作..但無論如何感謝.... – Siddharth 2013-03-01 12:08:51

相關問題