2013-02-20 5 views
0

현재이 클래스는 json urls를 구문 분석하고 Lazy Adapter Class 및 백그라운드 스레드의 도움으로 이미지와 텍스트를 목록보기로로드합니다.안드로이드 응용 프로그램 내에서 백그라운드 스레드 관리

각 목록 항목은 이미지보기와 2 개의 텍스트보기로 구성됩니다.

생성 된 각 목록 항목에 대해 팝업 상자 (알림 대화 상자)를 만들고 싶습니다. 경고 대화 상자에는 다른 응용 프로그램을 호출하는 옵션이 있습니다.

내 질문 :

는이 클래스에서이 경고 대화 기능을 코딩하는 것이 현명 할 것인가? 백그라운드에서 진행중인 작업이 많아서 앱의 기능에 영향을 줄 수 있습니다.

누군가가 다른 방법을 제안 할 수 없습니까? 감사.

JSON 활동 등급 :

public class JsonActivity extends SherlockActivity{ 

private ProgressDialog progressDialog; 

    // JSON Node names 


    static final String TAG_NAME = "name"; 
    static final String TAG_IMAGEURL = "imageurl"; 

    ListView list; 
    LazyAdapter adapter; 

    String chartUrl; 

    String[] urlNames = new String[] { 
      "urls..." 

      }; 

// chartItemList is the array list that holds the chart items 
    ArrayList<HashMap<String, String>> chartItemList = new ArrayList<HashMap<String, 
     String>>(); 

    //Holds imageurls 
    ArrayList<String> imageurls = new ArrayList<String>(); 


    JsonParser Parser = new JsonParser(); 
// JSONArray 
    JSONArray chartItems = null; 

    /** Called when the activity is first created. */  
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.chart); 

     //Get the bundle 
     Bundle bundle = getIntent().getExtras(); 

     //Extract the data from the bundle 
     int chartIndex = bundle.getInt("chartIndex"); 
     String chartUrl = urlNames[chartIndex]; 

     setTitle(bundle.getString("chartname")); 



     //url from where the JSON has to be retrieved 
     String url = chartUrl; 

     //Check if the user has a connection 

     ConnectivityManager cm = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo info = cm.getActiveNetworkInfo(); 
     if (info != null) { 
      if (!info.isConnected()) { 
       Toast.makeText(this, "Please check your connection and try again.", 
      Toast.LENGTH_SHORT).show(); 
      } 

      //if positive, fetch the articles in background 
      else new getChartItems().execute(chartUrl); 
     } 

     //else show toast 
     else { 
      Toast.makeText(this, "Please check your connection and try again.", 
      Toast.LENGTH_SHORT).show(); 
     } 

    } 


    class getChartItems extends AsyncTask<String, String, String> { 

     // Shows a progress dialog while setting up the background task 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progressDialog = new ProgressDialog(JsonActivity.this); 
      progressDialog.setMessage("Loading chart..."); 
      progressDialog.setIndeterminate(false); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 
     } 

     //Gets the json data for chart items data and presents it in a list view 
     @Override 
     protected String doInBackground(String... args) { 

     String json = Parser.getJSONFromUrl(args[0]); 
      String imageurl; 
     String rank; 
     String name;    
     String url; 

      try{ 


      chartItems = new JSONArray(json); 

      JSONObject json_data=null; 

      for(int i=0;i<chartItems.length();i++){ 

       json_data = chartItems.getJSONObject(i); 

       //Retrieves the value of the name from the json object 

       name=json_data.getString("name"); 

       //Retrieves the image url for that object and adds it to an arraylist 
       imageurl=json_data.getString("imageurl"); 
       //imageurls.add(imageurl); 

       HashMap<String, String> hashMap = new HashMap<String, String>(); 
       // adding each child node to HashMap key => value 
       //hashMap.put(TAG_RANK, rank); 
       hashMap.put(TAG_NAME, name); 
       hashMap.put(TAG_IMAGEURL, imageurl); 


       // adding HashMap to ArrayList 
        chartItemList.add(hashMap); 

      } 


       ; 
      } 

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

      runOnUiThread(new Runnable() { 
       public void run() { 


        list=(ListView)findViewById(R.id.list); 

        // Getting adapter by passing xml data ArrayList 
        adapter = new LazyAdapter(JsonActivity.this, chartItemList); 
        list.setAdapter(adapter); 

        // Click event for single list row 
        list.setOnItemClickListener(new OnItemClickListener() { 

         @Override 
         public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

         } 
        }); 




       } 
      }); 
      return null; 
     } 

     //Removes the progress dialog when the data has been fetched 
     protected void onPostExecute(String args) { 
      progressDialog.dismiss(); 
     } 
    } 

}

답변

1

이에 대한 나의 대답은 네, 지금까지 사용 사례가 그것을 정당화로 하나 개 더 수준의 네트워크 통신을 구현할 수있을만큼 현명하다.

이것은 통신 채널 (EDGE/3G/4G/WiFi) 및 애플리케이션의 사용 사례에 따라 다릅니다. 기술적으로 이것은 당신이 백그라운드에서 이것을하는 한 가능합니다. 또한로드중인 목록의 크기에 따라 다릅니다. 이를 확인하는 가장 좋은 방법은 플러그 가능한 코드를 구현하고 시험해 보는 것입니다.

관련 문제