0

I have an ArrayList called results that uses the class ItemObjects when I want to add an item in results. My problem is that I cannot manage to retrieve a specific String from an item.

The class code is:

public class ItemObjects {
    private String mText1;
    private String mText2;


    public ItemObjects(String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}

And I use the following code to add an item into the ArrayList:

ArrayList results = new ArrayList<ItemObjects>();
//THis part goes inside a for using i as increment;
ItemObjects obj = new ItemObjects(type, sender);
results.add(i , obj); 

I have tried several things to retrieve the data such as:

String type =  ItemObjects.getmText1();

or:

String type= results.get(i);

the first try, only retrieves the mText1 from the first item, and the second is an object and I dn't know how i should get the mText1 from it.

Any help would be appreciated :)

2 Answers 2

3

For adding the Value

ArrayList<ItemObjects> results = new ArrayList<ItemObjects>();
ItemObjects obj = new ItemObjects(type, sender);
results.add(obj);

For getting the Value

for (int i=0, i<result.size(); i++)
{
    String type =  results.get(i).mText1;
    String sender=  results.get(i).mText2; 

    Toast.makeText(this, "" + type, Toast.LENGTH_LONG).show();
    Toast.makeText(this, "" + sender, Toast.LENGTH_LONG).show();

}
Sign up to request clarification or add additional context in comments.

Comments

1

Change

ArrayList results = new ArrayList<ItemObjects>();

to

ArrayList<ItemObjects> results = new ArrayList<>();

Then results.get() will return ItemObjects.

Or you can simply cast your current result.get() to ItemObjects

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.