将位图添加到GridView

时间:2017-05-27 19:01:17

标签: android gridview

我试图弄清楚它为什么不起作用。带有R.id.drawables的适配器(不包含在代码中)工作正常。 简单地将位图放入ImageView也可以。 我认为问题在我的适配器,但我不知道在哪里。 任何帮助将不胜感激。感谢。

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>

1 个答案:

答案 0 :(得分:0)

尝试在图像加载完成后调用适配器

  

gridData.setAdapter(new BitmapAdapter(MainActivity.this,bitmaps));

export class Joke{
value: string;