从RealmDatabase检索数据并将其设置为GridView

时间:2017-08-14 10:05:23

标签: android json gridview realm

我能够将JSON数据保存到Realm数据库。我用作Realm的文档,但我无法将数据设置为GridView。我使用自定义适配器而不是领域适配器。数据已记录,但它们未显示在GridView中。如何将数据检索并显示到GridView?

PopularDestinationGridDetail 这是解析JSON数据并保存到数据库的地方

 Realm.init(this);
        realm = Realm.getDefaultInstance();
  LinearAddTOFavourite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savetoDatabase();
            }
        });


    public void savetoDatabase() {

        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                RealmDatabasePopularDestination realmDatabasePopularDestination = bgRealm.createObject(RealmDatabasePopularDestination.class);
                realmDatabasePopularDestination.setTitle(title);
                realmDatabasePopularDestination.setTitle(latitude);
                realmDatabasePopularDestination.setTitle(longitude);
                realmDatabasePopularDestination.setImage(image);

//                Toast.makeText(this,  realmDatabasePopularDestination.setLatitude(realmDatabasePopularDestination1.getLatitude()))


            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {

                Log.v("Success",title);


            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Log.e("failed", error.getMessage());
            }
        });


    }

收藏夹

public class Favourites extends Fragment {

    Realm realm;
    GridView gridViewBookmark;
    ArrayList<RealmDatabasePopularDestination> destination_bookmark_realm = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        Realm.init(getContext());
        realm = Realm.getDefaultInstance();


        RealmDatabasePopularDestination realmDatabasePopularDestination = new RealmDatabasePopularDestination();


        View view = inflater.inflate(R.layout.bookmark_layout_gridview, container, false);
        gridViewBookmark = (GridView) view.findViewById(R.id.gridviewBookmark);
        destination_bookmark_realm.add(realmDatabasePopularDestination);
        getData();
        return view;

    }


    public void getData() {
        FavouriteAdapter favouriteAdapter = new FavouriteAdapter(getContext(), destination_bookmark_realm);
        gridViewBookmark.setAdapter(favouriteAdapter);

        RealmResults<RealmDatabasePopularDestination> result = realm.where(RealmDatabasePopularDestination.class).equalTo("Title","niyash temple").findAll();

        result.load();

        System.out.println("Result is" + result);

//        String output = "";
//        for (RealmDatabasePopularDestination realmDatabasePopularDestination : result) {
//
//
//            output += realmDatabasePopularDestination.toString();
//
//        }
//
//        System.out.println("output" + output);
//        System.out.println("Total size=" + result.size());

    }

}

getter and setter

public class RealmDatabasePopularDestination extends RealmObject {

    String Title;
    String Latitude;
    String Longitude;
    String image;



    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getLatitude() {
        return Latitude;
    }

    public void setLatitude(String latitude) {
        Latitude = latitude;
    }

    public String getLongitude() {
        return Longitude;
    }

    public void setLongitude(String longitude) {
        Longitude = longitude;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

FavouriteAdapter

public class FavouriteAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;
    String TAG = "HomeTab_adapter";


    public FavouriteAdapter(Context mContext, ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark) {
        super();
        this.mContext = mContext;
        this.clas_realm_bookmark = clas_realm_bookmark;
    }


    @Override
    public int getCount() {

        return clas_realm_bookmark.size();
    }

    @Override
    public Object getItem(int position) {
        return clas_realm_bookmark.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final FavouriteAdapter.Holder viewHolder;

        if (convertView == null) {
            // inflate the layout
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.bookmark_grid_list_item, parent, false);

            // well set up the ViewHolder
            //  viewHolder = new ClassScheduleStudentAdapter.Holder();
            viewHolder = new FavouriteAdapter.Holder();

            // viewHolder.popular_destintion_id = (TextView) convertView.findViewById(R.id.student_profile_subject);
            viewHolder.title = (TextView) convertView.findViewById(R.id.festivalName);
            viewHolder.imageLogo = (ImageView) convertView.findViewById(R.id.event_festival_main_image);


            //     Log.d(TAG, "@@ postion:" + position + " getTeacherName" + class_destination.get(position).getId());
            convertView.setTag(viewHolder);


        } else {
            // we've just avoided calling findViewById() on resource everytime
            // just use the viewHolder
            //  viewHolder = (ClassScheduleStudentAdapter.Holder) convertView.getTag();
            viewHolder = (Holder) convertView.getTag();
        }

        viewHolder.title.setText(clas_realm_bookmark.get(position).getTitle());


        Picasso.with(mContext).load(clas_realm_bookmark.get(position).getImage()).error(R.drawable.close).into(viewHolder.imageLogo);


        return convertView;
    }

    class Holder {
        TextView title;
        ImageView imageLogo;

    }


}

我没有收到任何错误,但它们没有在ListView上设置。这是第一次使用领域,所以不要去我做错的地方。

2 个答案:

答案 0 :(得分:1)

而不是

public class FavouriteAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;

您应该使用realm-android-adapters中指定的documentation RealmBaseAdapter

答案 1 :(得分:0)

在从数据库中提取数据之前,您正在将适配器设置为列表视图。

RealmResults<RealmDatabasePopularDestination> result = realm.where(RealmDatabasePopularDestination.class).equalTo("Title","niyash temple").findAll();
result.load();

FavouriteAdapter favouriteAdapter = new FavouriteAdapter(getContext(), destination_bookmark_realm);
gridViewBookmark.setAdapter(favouriteAdapter);

使用上面的代码和。

destination_bookmark_realm应该使用从数据库

获得的结果加载