Search code examples
javaandroidrealm

Realm result showing [dbRealm = proxy[{points:[1]}] instead of number


My TextView show is showing the realm results as [dbRealm = proxy[{points:[1]}], [dbRealm = proxy[{points:[2]}], [dbRealm = proxy[{points:[3]}], etc... everytime I click. How can I make it show only the number I want instead of [dbRealm = proxy[{points:[1]}]?

Java code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Realm.init(getActivity());

        RealmConfiguration realmConfig = new RealmConfiguration.Builder()
                .name("dbRealm.realm")
                .schemaVersion(0)
                .build();
        Realm.setDefaultConfiguration(realmConfig);

        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ConstraintLayout content2 = view.findViewById(R.id.content2);
        TextView show = view.findViewById(R.id.show);
        Button removebut = view.findViewById(R.id.removebut);

        final TextView pointsText = view.findViewById(R.id.pointsText);
        final int[] hit = {0};
        realm = Realm.getDefaultInstance();

        content2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                hit[0]++;
            pointsText.setText(String.valueOf(hit[0]));

                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(@NonNull Realm realm) {
                            final dbRealm getPoints = realm.createObject(dbRealm.class);
                            getPoints.setPoints(Arrays.toString(hit));
                            realm.insertOrUpdate(getPoints);
                        }
                    });

                    Toast.makeText(getActivity(), "Botão +1", Toast.LENGTH_SHORT).show();


            }
        });

        RealmResults<dbRealm> results = realm.where(dbRealm.class).findAllAsync();
        results.load();

        removebut.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(@NonNull Realm realm) {
                        RealmResults<dbRealm> results = realm.where(dbRealm.class).findAllAsync();
                        results.load();
                        results.deleteAllFromRealm();
                    }
                });
                Toast.makeText(getActivity(), "Removido", Toast.LENGTH_SHORT).show();
            }
        });
        show.setText(results.toString());
        return view;

    }

dbRealm.java:

import io.realm.RealmObject;

/**
 * Created by MateusPC1 on 19/03/2018.
 */

public class dbRealm extends RealmObject{

    private String points;

    public String getPoints() {
        return points;
    }

    void setPoints(String points) {
        this.points = points;
    }
}

Solution

  • Instead of show.setText(results.toString());, you should be showing the value in the object obtained from the results.

    So something like

    dbRealm pointObject = results.get(0);
    show.setText(pointObject.getPoints());
    

    Although your model probably doesn't actually model what you want to do. Probably you might want to do something like

    public class dbRealm extends RealmObject{
        @PrimaryKey
        private String id; // null is a valid id and we will use it here
    
        private RealmList<Long> points; // Realm 4.0.0+
    
        public RealmList<Long> getPoints() {
            return points;
        }
    }
    
    Realm.setDefaultConfiguration(new RealmConfiguration.Builder()
             .initialData((realm) -> {
                 realm.createObject(dbRealm.class, null);
             })
             .deleteIfMigrationNeeded() // todo: make sure you can have this
             .build();
    

    and then

        content2.setOnClickListener(new View.OnClickListener(){
            ...
    
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(@NonNull Realm realm) {
                            final dbRealm getPoints = realm.where(dbRealm.class).findFirst();
                            RealmList<Long> points = getPoints.getPoints();
                            points.clear();
                            points.add(hit[0]);
                        }
                    });
    
        removebut.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(@NonNull Realm realm) {
                         final dbRealm getPoints = realm.where(dbRealm.class).findFirst();
                         RealmList<Long> points = getPoints.getPoints();
                         points.clear();
                    }
               });
           }