2017-05-27 148 views
0

我試圖找出爲什麼它不起作用。適配器與R.id.drawables(不包括在代碼中)工作正常。 只需將位圖放入ImageView中即可。 我認爲問題出在我的適配器上,但我不知道在哪裏。 任何幫助將不勝感激。謝謝。將位圖添加到GridView

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.gson.JsonObject; 
import com.koushikdutta.async.future.Future; 
import com.koushikdutta.async.future.FutureCallback; 
import com.koushikdutta.ion.Ion; 
import com.koushikdutta.ion.ProgressCallback; 
import com.squareup.picasso.Picasso; 

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.ExecutionException; 

public class MainActivity extends Activity { 
    private Button chuckNorrisButton; 
    private Button catButton; 
    private ProgressBar progressBar; 
    private TextView downloadCount; 
    private TextView quoteTextView; 
    GridView gridData; 
    Future<JsonObject> downloading; 
    public List<Bitmap> bitmaps = new ArrayList<>(); 
    //public List<Integer> bitmapsInt = new ArrayList<>(); 

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

     chuckNorrisButton = (Button)findViewById(R.id.donloadChuckNorrisButton); 
     catButton = (Button)findViewById(R.id.downloadCatButton); 
     progressBar = (ProgressBar)findViewById(R.id.progressBar3); 
     downloadCount = (TextView)findViewById(R.id.downloadCount); 
     quoteTextView = (TextView)findViewById(R.id.quoteTextView); 
     gridData = (GridView)findViewById(R.id.gallery_grid); 
     //gridData.setAdapter(new BitmapAdapter(getBaseContext(), bitmaps)); 


     catButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Ion.with(MainActivity.this) 
         .load("http://25.media.tumblr.com/tumblr_lpqn543evY1qb1nczo1_500.jpg")//("http://thecatapi.com/api/images/get?format=src") 
         .progressBar(progressBar) 
         .progressHandler(new ProgressCallback() { 
          @Override 
          public void onProgress(long downloaded, long total) { 
           downloadCount.setText("" + downloaded + "/" + total); 
          } 
         }) 
         .asBitmap() 
         .setCallback(new FutureCallback<Bitmap>() { 
          @Override 
          public void onCompleted(Exception e, Bitmap result) { 
           if (e != null) { 
            Toast.makeText(MainActivity.this, "Error downloading file", Toast.LENGTH_LONG).show(); 

            return; 
           } 
           //IT WORKS 
           //ImageView imageView = (ImageView)findViewById(R.id.imageView2); 
           //imageView.setImageBitmap(result); 


           bitmaps.add(result); 
          gridData.setAdapter(new BitmapAdapter(MainActivity.this, bitmaps)); 
           Toast.makeText(MainActivity.this, "File upload complete", Toast.LENGTH_LONG).show(); 

          } 
         }); 


      } 

     }); 

    } 

} 

而且我的適配器

public class BitmapAdapter extends BaseAdapter { 

    private Context mContext; 
    List<Bitmap> gridViewitems; 

    public BitmapAdapter(Context c, List<Bitmap> gridViewitems){ 

     mContext = c; 
     this.gridViewitems=gridViewitems; 
    } 

    @Override 
    public int getCount() { 
     return 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 

     if (convertView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } 
     else 
     { 
      imageView = (ImageView) convertView; 
     } 
     imageView.setImageBitmap(gridViewitems.get(position)); 
     return imageView; 
    } 


} 

@Edit 也許問題出在我的佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.rafal.restapidownload.MainActivity"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/quoteTextView"> 

     <GridView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:numColumns="auto_fit" 
      android:stretchMode="columnWidth" 
      android:gravity="center" 
      android:id="@+id/gallery_grid" /> 
    </ScrollView> 

    <Button 
     android:text="Cat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/downloadCatButton" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" /> 

    <Button 
     android:text="Quote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/donloadChuckNorrisButton" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:inputType="textMultiLine" 
     android:maxLines="10" 

     android:ems="10" 
     android:id="@+id/quoteTextView" 
     android:layout_below="@+id/donloadChuckNorrisButton" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentEnd="true" /> 

    <ProgressBar 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/progressBar3" 
     android:layout_above="@+id/quoteTextView" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/downloadCount" 
     android:layout_alignParentTop="true" 
     android:layout_alignEnd="@+id/progressBar3" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@android:drawable/btn_dialog" 
     android:id="@+id/imageView2" 
     android:layout_marginTop="109dp" 
     android:layout_below="@+id/quoteTextView" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 
+0

是什麼問題? – jagapathi

+0

位圖不顯示在我的GridView中 - 什麼也沒有發生 – PoorGuy

+0

您需要在加載圖像後設置您的適配器 – jagapathi

回答

0

嘗試調用適配器後圖像加載完成

gridData.setAdapter(新BitmapAdapter(MainActivity.this,位圖));

​​
+0

仍然沒有發生 – PoorGuy

+0

您可以編輯問題並添加更改的內容 – jagapathi

+0

我在第一篇文章中更改了代碼。 – PoorGuy