0

我目前在下面有这个类,它在惰性适配器类和后台线程的帮助下解析 json url 并将图像和文本加载到列表视图中。

每个列表项由一个图像视图和 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();
        }
    }

}

4

1 回答 1

1

我对此的回答是肯定的,只要你的用例证明它是合理的,实现一个更高级别的网络通信就足够明智了。

这取决于通信通道(EDGE/3G/4G/WiFi)和应用程序的用例。从技术上讲,只要您在后台执行此操作,这几乎是可能的。它还取决于您正在加载的列表的大小。检查这一点的最佳方法是实现可插入代码并尝试一下。

于 2013-02-20T09:20:37.173 回答