Question

I have a listView that displays the records from a database, and a setOnClickListener() for the items that pulls the text from the list and adds the String as an extra for an Intent.

This all works fine, my list displays records with Text such as, City, Town, Village, and when i add new items this also works fine. My issue lies with when i use a toast to display the string passed to the intent, some extra Chars are added

e.g. I click on the List item "town" and the toast displays "{pg=town}" how to i stop this from happening/ why is it happening?

The code for the Adapter and onClick is as Follows.

Thanks in advance.

public void listChange() {
        Cursor c = database.rawQuery("select * from Towns", null);
        c.moveToFirst();
        primeTownlist = new ArrayList<HashMap<String, String>>();
        while (!c.isAfterLast()) {
            primeTownMap = new HashMap<String, String>();
            primeTownMap.put("pg", c.getString(1));
            primeTownlist.add(primeTownMap);
            c.moveToNext();
        }
        c.close();
        primeGoalAdapter = new SimpleAdapter(this, primeTownlist, R.layout.primeTowns,
                new String[] { "pg" }, new int[] {R.id.bt_primeTown});
        primeTowns.setAdapter(primeTownAdapter);
    }

primeTowns.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> list, View view, int position, long id) {
                String title = list.getItemAtPosition(position).toString();
                Intent i = new Intent("com.example.towns.town");
                i.putExtra("town", title);
                startActivity(i);
                Toast.makeText(getApplicationContext(),
                        (title), Toast.LENGTH_SHORT).show();

                }

        });
Était-ce utile?

La solution

That because your list is a list of HashMap. Try

String title = ((HashMap<String,String>) list.getItemAtPosition(position)).get("pg");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top