【问题标题】:Retrieve the Data From the RealmDatabase and Set it to GridView从 RealmDatabase 中检索数据并将其设置为 GridView
【发布时间】:2017-08-14 10:05:22
【问题描述】:

我能够将 JSON 数据保存到领域数据库。我已经用作 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 和 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;
    }
}

FavoriteAdapter

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 上设置。这是第一次使用领域,所以不要知道我做错了什么。

【问题讨论】:

    标签: android json gridview realm


    【解决方案1】:

    代替

    public class FavouriteAdapter extends BaseAdapter {
    
        Context mContext;
        ArrayList<RealmDatabasePopularDestination> clas_realm_bookmark = null;
    

    您应该使用realm-android-adapters 中的RealmBaseAdapter,如documentation 中指定的那样。

    【讨论】:

    • RealmBaseAdapter 是强制性的还是我们可以使用任何自定义适配器???
    • 好吧,您可以使用任何适配器,只要您使用RealmResults&lt;T&gt; 而不是ArrayList,并且您还可以在数据集更改时正确管理RealmChangeListener 调用正确的adapter.notify* 方法。如果您不想手动管理RealmChangeListener,请使用RealmBaseAdapter
    • 谢谢,这对我也有帮助。 ,反正我已经用自定义适配器解决了。
    • 现在我想知道如果您从领域中删除其中一个对象然后单击该行会发生什么...
    • 我现在还没有检查过。会发生什么,建议。你以前做过这种问题吗。如果我用带有领域的适配器替换自定义适配器不会有问题,用相同的代码?
    【解决方案2】:

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

    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 它应该与您从数据库获得的结果一起加载

    【讨论】:

    • 得到这样的输出 I/System.out: 结果是[RealmDatabasePopularDestination = proxy[{Title:85.3404},{Latitude:null},{Longitude:null},{image:192.168.100.7:1337/images/popular_places/Patan-2.jpg} ]] 08-14 16:05:48.704 16347-16347/org.municipality.mobile.patanheritage W/Settings:设置飞机模式已从 android.provider.Settings.System 移动到 android.provider.Settings.Global,返回只读价值。
    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多