2016-08-22 11 views
0

私の主な活動ではないことに注意してください。今はデータベースから画像を取得するためにボタンを使用しています。アプリの読み込み。手作業でアプリケーションを読み込んだときにデータベースからすべての画像をロードする

public class QuotesPictures extends Activity implements View.OnClickListener { 

    private String imagesJSON; 

    private static final String JSON_ARRAY = "result"; 
    private static final String IMAGE_URL = "url"; 
    private JSONArray arrayImages = null; 
    private int TRACK = 0; 
    private Button buttonMoveNext; 
    private Button buttonMovePrevious; 
    private ImageView imageView; 
    private static final String IMAGES_URL = "http://stressreliefapp.esy.es/getAllImages.php"; 
    private Button buttonFetchImages; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_quotes_pictures); 

     //Defining all the buttons 
     imageView = (ImageView) findViewById(R.id.imageView); 
     buttonFetchImages = (Button) findViewById(R.id.buttonFetchImages); 
     buttonMoveNext = (Button) findViewById(R.id.buttonNext); 
     buttonMovePrevious = (Button) findViewById(R.id.buttonPrev); 
     buttonFetchImages.setOnClickListener(this); 
     buttonMoveNext.setOnClickListener(this); 
     buttonMovePrevious.setOnClickListener(this); 
    } 

    //Using AsyncTask to load the data in the background thread and then publishing on the UI thread 
    private void getImage(String urlToImage) { 
     class GetImage extends AsyncTask<String, Void, Bitmap> { 
      ProgressDialog loading; 

      @Override 
      protected Bitmap doInBackground(String... params) { 
       URL url = null; 
       Bitmap image = null; 

       String urlToImage = params[0]; 
       try { 
        url = new URL(urlToImage); 
        image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return image; 
      } 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(QuotesPictures.this, "Loading Images...", "Please wait...", true, true); 
      } 

      @Override 
      protected void onPostExecute(Bitmap bitmap) { 
       super.onPostExecute(bitmap); 
       loading.dismiss(); 
       imageView.setImageBitmap(bitmap); 
      } 
     } 

     GetImage gi = new GetImage(); 
     gi.execute(urlToImage); 
    } 

    // Method used to get all the images from the database using AsyncTask 
    public void getAllImages() { 
     class GetAllImages extends AsyncTask<String, Void, String> { 
      ProgressDialog loading; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(QuotesPictures.this, "Loading Images...", "Please wait...", true, true); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       imagesJSON = s; 
       extractJSON(); 
       showImage(); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       String uri = params[0]; 
       BufferedReader bufferedReader = null; 
       try { 
        URL url = new URL(uri); 
        HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
        StringBuilder sb = new StringBuilder(); 

        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

        String json; 
        while ((json = bufferedReader.readLine()) != null) { 
         sb.append(json + "\n"); 
        } 

        return sb.toString().trim(); 

       } catch (Exception e) { 
        return null; 
       } 
      } 
     } 
     GetAllImages gai = new GetAllImages(); 
     gai.execute(IMAGES_URL); 
    } 

    private void extractJSON() { 
     try { 
      JSONObject jsonObject = new JSONObject(imagesJSON); 
      arrayImages = jsonObject.getJSONArray(JSON_ARRAY); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void showImage() { 
     try { 
      JSONObject jsonObject = arrayImages.getJSONObject(TRACK); 
      getImage(jsonObject.getString(IMAGE_URL)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    private void moveNext() { 
     if (TRACK < arrayImages.length()) { 
      TRACK++; 
      showImage(); 
     } 
    } 

    private void movePrevious() { 
     if (TRACK > 0) { 
      TRACK--; 
      showImage(); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     if (v == buttonFetchImages) { 
      getAllImages(); 
     } 
     if (v == buttonMoveNext) { 
      moveNext(); 
     } 
     if (v == buttonMovePrevious) { 
      movePrevious(); 
     } 
    } 
} 

答えて

0

だけのonCreate方法であなたのダウンロード方法を実行します。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quotes_pictures); 

    //Defining all the buttons 
    imageView = (ImageView) findViewById(R.id.imageView); 
    buttonFetchImages = (Button) findViewById(R.id.buttonFetchImages); 
    buttonMoveNext = (Button) findViewById(R.id.buttonNext); 
    buttonMovePrevious = (Button) findViewById(R.id.buttonPrev); 
    buttonFetchImages.setOnClickListener(this); 
    buttonMoveNext.setOnClickListener(this); 
    buttonMovePrevious.setOnClickListener(this); 

    getImage("your image url"); 
} 
+0

ありがとう、私はそれを理解する一日を費やすと信じられず、解決はとても簡単でした。君はスター!!! –

0
onResumeに onclicklistener であなたのコードを移動し

public void onResume() 
{ 
    super.onResume(); 
    /* your code at OnClickListener */ 
} 
関連する問題