0

I have an ArrayList in a TextView that prints out all the names of my list from sqlite. It is printing the values

{saleNotes=apples} 
{saleNotes=oranges} 

etc in my TextView. I want my TextView to look like this instead.

apples
oranges

Here is my Database info

public ArrayList<HashMap<String, String>> getAllsalesnames1() {
        ArrayList<HashMap<String, String>> wordList2;

        wordList2 = new ArrayList<HashMap<String, String>>();
        String selectQuery2 = "SELECT DISTINCT salesNotes FROM QUICK";
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor2 = database.rawQuery(selectQuery2, null);
        if (cursor2.moveToFirst()) {
        do {
        HashMap<String, String> map2 = new HashMap<String, String>();
        map2.put("salesNotes", cursor2.getString(0));
        wordList2.add(map2);
        }    
                while (cursor2.moveToNext());
        }
        database.close();
        return wordList2 ;
    }

And here in my Activity is how I get it from the database and make the changes to allow ArrayList into TextView

 ArrayList<HashMap<String, String>> saleList1 =  controller.getAllsalesnames1();
    if(saleList1.size()!=0) {
    String listString = "";
    for (HashMap<String, String> s : saleList1)
    {listString += s + "\n";}
    System.out.println(listString);
    saleNotes.setText(listString);}

Any ideas what I am doing wrong? This works fine except for the unwanted {saleNotes=} .I have attempted to remove that by creating String x={saleNotes=} and then tried to remove x from saleList1 but that didn't work.

2 Answers 2

2

Because "s" is a HashMap and by printing it out you are printing out the toString() of your Map.

Instead try using {listString += s.get("salesNotes") + "\n";}

(getting the value based on the key of salesNotes)

P.S. one wonders why you are using a single entry HashMap as an element in an ArrayList? Are there other entries in the HashMap that are not included in this question?

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

2 Comments

@James W i have a small doubt If any other key will comes with in the map.
@newuser sorry for late reply, yes if there are other keys then dont use a static string. That was just for example purposes.
1

Try StringUtils.substringAfter , StringUtils.substringBetween

s  = "Sample=Apple";
StringUtils.substringAfter(s, "="); // if it is not having {}

s  = "{Sample=Apple}";
StringUtils.substringBetween(s, "=", "}"); // if it having {}

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.