-2

I'm making an application, where I have a central recyclerview where items that the user will type are listed, below 2 buttons, one for adding items and the other for finishing. The problem I am having is that when the user finishes the task and clicks on finish I want to get all the data that was listed on his screen that is inside a List and store it in firebase, but I am not able to get this data in shape array, it always returns the object reference.

Searching the internet I found how to use the .toString in the array, but it returns me the same thing. Here is the example of the return I am receiving.

return

the code of my object

public class ListItem {
    private float measure;

    public ListItem(float measure) {
        this.measure = measure;
    }

    public float getMeasure() {
        return measure;
    }

    public void setMeasure(float measure) {
        this.measure = measure;
    }
}

the code of my button add

private View.OnClickListener addItem = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogForm();

            mAdapter.notifyItemInserted(listItems.size());
        }
    };
void DialogForm() {
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        final View customLayout = getLayoutInflater().inflate(R.layout.layout_dialog, null);
        alert.setView(customLayout);

        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EditText measure = customLayout.findViewById(R.id.edit_measure);
                String response = measure.getText().toString();
                if (response.isEmpty() || response.equals("0")) {
                    Toast.makeText(MainActivity.this, "A medida não pode ser vazia",
                            Toast.LENGTH_SHORT).show();
                } else {
                    listItems.add(new ListItem(Float.parseFloat(response)));
                    btnFinish.setEnabled(true);
                    btnFinish.setBackgroundResource(R.drawable.bg_button_enabled);
                }
            }
        });

        AlertDialog dialog = alert.create();
        dialog.show();
    }

the code of my button finish, when I'm listing

private View.OnClickListener finishListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            for (int i=0; i<listItems.size(); i++){
                Log.d("teste", listItems.get(i).toString());
            }
        }
    };
Dharman
  • 30,962
  • 25
  • 85
  • 135
csevero
  • 361
  • 1
  • 8
  • You basically need to implement a `toString()` method for `ListItem` that creates a string that displays the item how you want it to be displayed. This might be sufficient for your purposes: `public String toString() { return "" + measure; }` – Stephen C Jul 12 '20 at 07:38
  • @StephenC thanks for that link, I would absolutely have voted to close for that duplicate if I'd known about it. Should this be closed as a duplicate of that question, then? – Ryan M Aug 03 '21 at 08:38

1 Answers1

0

toString() will always return the hash of the object if not override as it is an inherited method from java.lang.Object and not specific to your object

If you want toString to return the measure variable then add the following to your ListItem class

@Override
public String toString() {
    return Integer.toString(measure);
}

In order to add more entries to the method, just add another + myData but perhaps ensure there is some sort of divider to make it moire readable - such as + " || " + or something

Good luck with the project

Emily-TTG
  • 531
  • 3
  • 18