2013-05-15 2 views
0

메신저해서 ProgressDialog를 만들 필요가 자신의 콘텐츠를 main_screen.xmlJSON 구문 분석 데이터 전에 안드로이드 JSON 구문 분석 작업 전에 메인 화면 활동을 만들려고

public class AndroidJSONParsingActivity extends ListActivity { 


    // url to make request 
    private static String url = "http://api.androidhive.info/contacts/"; 

    // JSON Node names 
    private static final String TAG_CONTACTS = "contacts"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_EMAIL = "email"; 
    private static final String TAG_ADDRESS = "address"; 

    // contacts JSONArray 
    JSONArray contacts = null; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Hashmap for ListView 
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

     try { 
      // Getting Array of Contacts 
      contacts = json.getJSONArray(TAG_CONTACTS); 

      // looping through All Contacts 
      for(int i = 0; i < contacts.length(); i++){ 
       JSONObject c = contacts.getJSONObject(i); 

       // Storing each json item in variable 
       String id = c.getString(TAG_ID); 
       String name = c.getString(TAG_NAME); 
       String email = c.getString(TAG_EMAIL); 
       String address = c.getString(TAG_ADDRESS); 

       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(TAG_ID, id); 
       map.put(TAG_NAME, name); 
       map.put(TAG_EMAIL, email); 
       map.put(TAG_ADDRESS, address); 

       // adding HashList to ArrayList 
       contactList.add(map); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 


     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(this, contactList, 
       R.layout.list_item, 
       new String[] { TAG_NAME, TAG_EMAIL, TAG_ADDRESS }, new int[] { 
         R.id.name, R.id.email, R.id.address }); 

     setListAdapter(adapter); 

     // selecting single ListView item 
     ListView lv = getListView(); 

     // Launching new screen on Selecting Single ListItem 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // getting values from selected ListItem 
       String name = ((TextView) view.findViewById(R.id.name)).getText().toString(); 
       String email = ((TextView) view.findViewById(R.id.email)).getText().toString(); 
       String address = ((TextView) view.findViewById(R.id.address)).getText().toString(); 

       // Starting new intent 
       Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
       in.putExtra(TAG_NAME, name); 
       in.putExtra(TAG_EMAIL, email); 
       in.putExtra(TAG_ADDRESS, address); 
       startActivity(in); 

      } 
     });  

    } 
} 
AndroidJSONParsingActivity.java

<?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" 
    android:gravity="center_horizontal"> 

    <!-- Sample Dashboard screen with Two buttons --> 
    <Button android:id="@+id/btnViewProducts" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="View Products" 
     android:layout_marginTop="25dip"/> 
</LinearLayout> 

입니다

json 파서 데이터보다 먼저 ProgressDialog를 만들고 구문 분석 데이터가 완료되면 닫아야합니다.

Android에서 새롭게 출시되었습니다. 고맙습니다.

+0

AsyncTask를 사용하십시오. 이 링크 http://developer.android.com/reference/android/os/AsyncTask.html을 확인하십시오. –

답변

0

시도해주세요. AsyncTask를 사용하고 아래 코드를 사용하십시오.

private ProgressDialog progressDialog; // class variable 

private void showProgressDialog(String title, String message) 
{ 
     progressDialog = new ProgressDialog(this); 

     progressDialog.setTitle(title); // set title 

     progressDialog.setMessage(message); // set message 

     progressDialog.setCancelable(false); 

     progressDialog.show(); 
} 

onPreExecute()

showProgressDialog("Please wait...", "Connecting to server for data"); 

onPostExecute()

if(progressDialog != null && progressDialog.isShowing()) 
    { 
    progressDialog.dismiss(); 
    } 
0

를 사용하여 비동기 작업은

그래서 당신이 dilog 진행을 시작할 수 있습니다, JSON 데이터를 다운로드 사전 실행 방법 및 사후 실행 메소드에서 중지하십시오.