2013-02-20 85 views
0

我目前有这个类,它解析json网址,并借助Lazy Adapter类和后台线程将图像和文本加载到列表视图中。管理Android应用程序中的后台线程

每个列表项都包含一个图像视图和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)和应用程序的用例。从技术上讲,只要你在背景中做这件事,这是非常可能的。它也取决于你正在加载的列表的大小。最好的方法来检查这是通过实现可插入代码并尝试。