2017-08-15 2 views
0

この行にエラーがあります。 - >新しいGetHttpResponse(this).execute();それはボタンsetOnClickListener内でAsynctaskを実行できません。

私はhttp://www.android-examples.com/create-dynamic-listview-using-json-parsing-php-mysql/からこのコードを持って、私はちょうどデータベースが更新されたときにそれがリフレッシュすることができるようにボタンを追加しました外に置かれたとき

は、それがエラーを持っているdoens't

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); 
      } 
     } 
    } 
} 
+1

「どちらか」がどちらの場合でも –

答えて

2

インサイドOnClickListener、thisは、予想されるコンテキストオブジェクトではなく、OnClickListenerのインスタンスを表します。したがって、thisMainActivity.thisである必要があります。

new GetHttpResponse(MainActivity.this).execute(); 
関連する問題