自定义GridView不显示数据

时间:2017-06-23 17:28:25

标签: java android facebook-graph-api gridview

我正在尝试使用从Facebook Graph API获取的数据来提供我的自定义GridView。该数据是包含当前用户的专辑的图片和专辑的名称的专辑对象的列表。 获取数据并不代表问题,因为我可以在LOGCAT中看到它。 似乎是一个问题是我的GridView适配器,我想,程序没有给出任何特定的错误,这让我感到困惑。 请注意,此处不显示的MainActivity仅用于通过Facebook帐户连接用户(此活动没有问题) 这是我的不同课程: 的 ---- LoggedInActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LoggedInActivity extends Activity {

    String name, surname, ID;
    TextView nameOfUser;
    Button logout;
    GridView gridView;
    GridViewAdapter gridViewAdapter;
    List<Album> albumList = new ArrayList<Album>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle inBundle = getIntent().getExtras();
        name = inBundle.getString("name");
        surname = inBundle.getString("surname");
        ID = inBundle.getString("ID");
        setContentView(R.layout.activity_logged_in);
        nameOfUser = (TextView) findViewById(R.id.name);
        nameOfUser.setText(name + " " + surname);
        logout = (Button) findViewById(R.id.logout);
        gridView = (GridView) findViewById(R.id.myGridView);

        albumList = getAlbums();
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginManager.getInstance().logOut();
                Intent login = new Intent(LoggedInActivity.this, MainActivity.class);
                startActivity(login);
                finish();
            }
        });


        gridViewAdapter = new GridViewAdapter(getBaseContext(), albumList);
        gridView.setAdapter(gridViewAdapter);
}
    public List<Album> getAlbums() {


        albumList = new ArrayList<Album>();
        GraphRequest request = GraphRequest.newGraphPathRequest(
                AccessToken.getCurrentAccessToken(),
                "/me/albums",
                new GraphRequest.Callback() {
                    Album album = null;
                    @Override
                    public void onCompleted(GraphResponse response) {
                        JSONObject jsonDATA = response.getJSONObject();
                        JSONArray data = jsonDATA.optJSONArray("data");
                        for(int i = 0 ; i< data.length(); i++){
                            try {
                                JSONObject e = data.getJSONObject(i);
                                String name = e.optString("name");
                                JSONObject picture = e.getJSONObject("picture");
                                JSONObject dataOfPicture = picture.getJSONObject("data");
                                String url = dataOfPicture.optString("url");
                                album = new Album(name, url);
                                albumList.add(album);
                                Log.d("Mine",name + url);
                            } catch (JSONException e1) {
                                Toast.makeText(getBaseContext(), "Problem with JSON parsing", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "picture,name");
        request.setParameters(parameters);
        request.executeAsync();

        return albumList;
    }
}

------ Album.java

public class Album {
    public String album_name;
    public String album_image;

    public Album(String album_name, String album_image) {
        this.album_name = album_name;
        this.album_image = album_image;
    }

    public String getAlbum_name() {
        return album_name;
    }

    public String getAlbum_image() {
        return album_image;
    }

    public void setAlbum_name(String album_name) {
        this.album_name = album_name;
    }

    public void setAlbum_image(String album_image) {
        this.album_image = album_image;
    }
}

----- GridViewAdapter.java

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.URL;
import java.util.List;


public class GridViewAdapter extends BaseAdapter {
    private List<Album> albumList;
    private Context context;
    public GridViewAdapter(@NonNull Context context, @NonNull List<Album> objects) {
        this.albumList = objects;
        this.context = context;


    }

    @Override
    public int getCount() {
        return albumList.size();
    }

    @Nullable
    @Override
    public Album getItem(int position) {
        return albumList.get(position);
    }

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

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v = convertView;
        if(v==null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item,null);
        }
        Album album = getItem(position);
        ImageView imageView = (ImageView)v.findViewById(R.id.imageHolder);
        TextView description = (TextView)v.findViewById(R.id.albumDesc);
        try {
            imageView.setImageBitmap(BitmapFactory.decodeStream((new URL(album.getAlbum_image())).openConnection().getInputStream()));
        } catch (IOException e) {
            Toast.makeText(context, "Problem with input output streams", Toast.LENGTH_SHORT).show();
        }
        description.setText(album.getAlbum_name());


        return v;
    }


}

------ item.xml

<?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"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageHolder"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:layout_gravity="center"
            android:layout_weight="1"

            app:srcCompat="@drawable/com_facebook_profile_picture_blank_square" />

        <TextView
            android:layout_width="wrap_content"
            android:text="photo"
            android:textSize="15sp"
            android:id="@+id/albumDesc"
            android:layout_below="@+id/imageHolder"
            android:layout_marginLeft="20dp"
            android:layout_height="wrap_content" />



</RelativeLayout>

------ Activity_logged_in.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ichou.facebooktest.LoggedInActivity">

    <LinearLayout
        android:id="@+id/intro"
        android:layout_width="368dp"
        android:layout_height="40dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:paddingTop="2dp"
        android:paddingBottom="2dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp"
            android:paddingTop="10dp"
            android:text="username"
            android:id="@+id/name"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:text="logout"
            android:id="@+id/logout"/>


    </LinearLayout>
    <GridView
        android:layout_below="@+id/intro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myGridView"
        android:layout_marginTop="10dp"
        android:paddingTop="2dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:numColumns="2"
        android:columnWidth="150dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在下面的适配器中编写代码... 你在getView()方法

中的膨胀布局中做错了一行
 @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View v = convertView;
        if(v==null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.item,convertView, false);
        }
        Album album = getItem(position);
        ImageView imageView = (ImageView)v.findViewById(R.id.imageHolder);
        TextView description = (TextView)v.findViewById(R.id.albumDesc);
        try {
            imageView.setImageBitmap(BitmapFactory.decodeStream((new URL(album.getAlbum_image())).openConnection().getInputStream()));
        } catch (IOException e) {
            Toast.makeText(context, "Problem with input output streams", Toast.LENGTH_SHORT).show();
        }
        description.setText(album.getAlbum_name());


        return v;
    }