AndroidNewbie
AndroidNewbie

Reputation: 669

Fragment implementing AsyncTask and Listview in android

Here is my Java code

but it doesnt work i dont get my error. Snpashot

package info.androidhive.slidingmenu;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;   
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class CommunityFragment extends Fragment {


    public CommunityFragment(){}

    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_community, container, false);

        new DownloadJSON().execute();
        return rootView;
    }
    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(CommunityFragment.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Student Government App");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONFunctions
                    .getJSONfromURL("http://app-dlslsg.azurewebsites.net/json/postList.php");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("post");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("rank", jsonobject.getString("id"));
                    map.put("country", jsonobject.getString("body"));
                    map.put("population", jsonobject.getString("stamp"));
                    map.put("flag", jsonobject.getString("image"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                    //Log.i("body",COUNTRY);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(CommunityFragment.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

}

I tried searching all over the net today but i don't get a chance to see any related problems. Will someone help me to figure out this? i will reply asap on those responses advance thanks for help!

Upvotes: 4

Views: 8087

Answers (2)

Leaudro
Leaudro

Reputation: 1021

A fragment is not a Context. You have to use getActivity() where you're using CommunityFragment.this.

replace

mProgressDialog = new ProgressDialog(CommunityFragment.this);

with

mProgressDialog = new ProgressDialog(getActivity());

and

adapter = new ListViewAdapter(CommunityFragment.this, arraylist);

with

adapter = new ListViewAdapter(getActivity(), arraylist);

Upvotes: 5

Jeffrey Klardie
Jeffrey Klardie

Reputation: 3028

Oke, so there are multiple errors:

  1. The ListViewAdapter constructor requires an Activity (or Context), but you are passing a Fragment. You can use Fragment.getActivity() to obtain the Activity for the Fragment, but only if it is attached.
  2. The ProgressDialog constructor requires a Context object, but again you pass a Fragment. A Fragment does not extend the Context, but the Activity class does. Again, you can pass Fragment.getActivity().
  3. Lastly, you try to call findViewById() form inside the AsyncTask. This is a method that is only available inside the Activity, so again you need to obtain the Activity and call the method via the Activity object.

Upvotes: 1

Related Questions