fluorescentadolescent
fluorescentadolescent

Reputation: 23

Unable to execute Asynctask inside the setOnClickListener

I'm having error on this line -> new GetHttpResponse(this).execute();

It doens't have error when it is put outside the button

I got this code from http://www.android-examples.com/create-dynamic-listview-using-json-parsing-php-mysql/ and i just added the button so it can be refreshed when database is updated

 public class MainActivity extends Activity {

    ListView listCollege;
    ProgressBar proCollageList;
    Button bton;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    bton = (Button) findViewById(R.id.button);
    listCollege = (ListView)findViewById(R.id.listCollege);
    proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
    new GetHttpResponse(this).execute();

    bton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "List is refreshed", Toast.LENGTH_SHORT).show();
            new GetHttpResponse(this).execute();
        }});
}



    private class GetHttpResponse extends AsyncTask<Void, Void, Void> 
    {
        private Context context;
        String result;
        List<benedict.com.listviewpractice.cources> collegeList;
        public GetHttpResponse(Context context)
        {
            this.context = context;
        }

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) 
        {
            HttpService httpService = new HttpService("http://10.0.2.2/courses.php");
            try 
            {
                httpService.ExecutePostRequest();

                if(httpService.getResponseCode() == 200)
                {
                    result = httpService.getResponse();
                    Log.d("Result", result);
                    if(result != null)
                    {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(result);

                            JSONObject object;
                            JSONArray array;
                            benedict.com.listviewpractice.cources college;
                            collegeList = new ArrayList<benedict.com.listviewpractice.cources>();
                            for(int i=0; i<jsonArray.length(); i++)
                            {
                                college = new benedict.com.listviewpractice.cources();
                                object = jsonArray.getJSONObject(i);

                                college.cources_name = object.getString("cource_name");
                                college.cources_description = object.getString("cources_description");

                                collegeList.add(college);
                            }
                        }
                        catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                else
                {
                    Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
            } 
            catch (Exception e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) 

        {
            proCollageList.setVisibility(View.GONE);
            listCollege.setVisibility(View.VISIBLE);
            if(collegeList != null)
            {
            ListAdapter adapter = new ListAdapter(collegeList, context);
            listCollege.setAdapter(adapter);
            }
        }
    }
}

Upvotes: 0

Views: 48

Answers (1)

Ray Wang
Ray Wang

Reputation: 106

Inside the OnClickListener, this represents instance of OnClickListener, not the context object that you expected, so this should be MainActivity.this.

new GetHttpResponse(MainActivity.this).execute();

Upvotes: 2

Related Questions