SmulianJulian
SmulianJulian

Reputation: 802

sqlite query returns column name and string

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.

Upvotes: 0

Views: 90

Answers (2)

James W
James W

Reputation: 410

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?

Upvotes: 2

newuser
newuser

Reputation: 8466

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 {}

Upvotes: 1

Related Questions