Search code examples
androidjsongridviewrealm

Retrieve the Data From the RealmDatabase and Set it to GridView


I am able to Save the JSON Data to the Realm Database. I have used as the documentation of the Realm, but I am not able to set the data to the GridView. I am using Custom Adapter not the Realm Adapter. The Data are Logged but they are not Displayed to the GridView. How can this the Data be Retrieved and Displayed to the GridView?

PopularDestinationGridDetail this is where JSON data is parsed and saved to database

 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());
            }
        });


    }

Favourites

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;

    }


}

I am not getting any error but they are not set on the ListView.This is the first time using realm, so don't get where I am doing wrong.


Solution

  • you are setting adapter to list view before extracting data from database.

    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);
    

    use above code and.

    destination_bookmark_realm it should be load with the result you got from databse