Question

I currently have this class below which parses json urls and loads images and texts into a listview with the help of the Lazy Adapter Class and background thread.

Each list item consists of an image view and 2 text views.

I want to create pop up boxes (alert dialog) for each of the generated list items. The alert dialog will have options which will call other applications.

My question :

Would it be wise to code this alert dialog functionality in this class? I'm worried that there is a lot of stuff currently being done in the background and it might affect the app's functionality.

If not could anyone suggest another way to do it. thanks.

Json Activity Class :

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();
        }
    }

}

Was it helpful?

Solution

My answer for this is Yes, it is wise enough to implement one more level network communication as far as your use case justifies it.

This depends on communication channel (EDGE/ 3G/ 4G/ WiFi) and use case of the application. Technically it is pretty much possible as far as you are doing this in background. It also depends on the size of the list which you are loading. Best way to check this is by implementing plug-able code and try it out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top