0

I´m trying to use setText method to show all the elements from an ArrayList of Objects into a TextView.

I have 6 buttons in 6 different shelves (A,B,C,D,E,F) with a popup menu each one, where the user chose a cardinal point (N,S,E,W) and after that show the shelve letter and the item clicked in a text view. This is the map:

Here is the code I´m using:

Button buttonA, buttonB, buttonC, buttonD, buttonE, buttonF;
private TextView coordenada_view;
ArrayList<PickUpPoint> pickuppoint_array = new ArrayList<PickUpPoint>();

    @Override
    protected void onCreate{......

buttonA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final PopupMenu popupMenu = new PopupMenu(Mapa.this, buttonA);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
          @Override
          public boolean onMenuItemClick(MenuItem item) {
          String mShelving = buttonA.getText().toString();
           String mCardinalPoint = item.getTitle().toString();

                 pickuppoint_array.add(new PickUpPoint(mShelving,mCardinalPoint));
                 PickUpPoint pickUpPoint = new PickUpPoint(mShelving,mCardinalPoint);

//Here I´m trying to show all the elements of the ArrayList into the TextView
                    for (int i = 0; i < pickuppoint_array.size(); i++){
                      coordenada_view.setText( pickuppoint_array.get(i).toString());
                       }
                        return true;
                    }
                });
                popupMenu.show();

buttonB.setOnClickListener....
buttonC.setOnClickListener....
}

This is the class PickUpPoint:

class PickUpPoint {
    public String shelving;
    public String cardinalPoint;

    public String getShelving() {
        return shelving;
    }

    public String getCardinalPoint() {
        return cardinalPoint;
    }

    PickUpPoint(String shelving, String cardinalPoint) {
        this.shelving = shelving;
        this.cardinalPoint = cardinalPoint;
    }
}

But I got this from the array list:

So, my question is...How to get all the elements from the array list like this?

2 Answers 2

1

Use this method to set text from Arraylist

Also check if Arraylist is not null and empty

public void setTextViewFromList(ArrayList<PickUpPoint> arraylist, TextView textview) {
    //Variable to hold all the values
    String output = "";

    for (int i = 0; i < arraylist.size(); i++) {
        //Append all the values to a string
        output += arraylist.get(i).getShelving();//whatever you want to show here like shelving or cordinalpoint use getCordinalPoint()
        output += "\n";
    }

    //Set the textview to the output string
   textview.setText(output);
}

USAGE

Call this method like this

setTextViewFromList(pickuppoint_array,coordenada_view)
Sign up to request clarification or add additional context in comments.

2 Comments

Tahnks Quick learner,trying to use your method but it doesn´t recognize getName() and append('\n') .
check my answer now
0

I assume that the coordenada_view#setTextcan only be called once.

So you need to convert the Elements in pickuppoint_array into a String first and then supply this to the setText method.

StringBuilder content = new StringBuilder();
for (int i = 0; i < pickuppoint_array.size(); i++) {
  content.append(pickuppoint_array.get(i).toString());
  content.append('\n');
}

coordenada_view.setText(content.toString());

You also want to implement the toString() method in your PickUpPoint class to return the desired representation of each object, for example:

public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("SHELVING ");
    builder.append(shelving);
    builder.append(" - ");
    builder.append(cardinalPoint);
    return builder.toString();
}

Ps.: If you know the expected length of these strings you should supply this as parameter to the StringBuilder constructor, so the jvm does not need to waste time with resizing it.

new StringBuilder(18) might work for the toString method, and new StringBuilder(19 * pickuppoint_array.size()) might work for the other one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.