Question

Here's what I'm trying to do: - Retrieve a Map of key-value pairs from a SharedPreferences object (User#, Name) - write those key-value pairs into an ArrayList such that I can - use them to populate a ListView with each row containing BOTH the key and the value like so:

  1. User 1 - Joe R.

  2. User 2 - Frank B.

etc


UPDATE: so after taking a good long look at the SimpleAdapter class, and talking with some wiser more knowledgable folks - I'm a lot closer than I was... but still not all the way there.

here's what I have now:

public class LogHistory extends ListActivity {

static final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
private static final String KEY = null;
private static final String VALUE = null;

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.log_history);

    SharedPreferences logPrefs = getSharedPreferences(LoginField.PREFSNAME, 0);
    Map<String, ?> logMap = logPrefs.getAll();

    for (Map.Entry<String, ?> e : logMap.entrySet()) {
        HashMap<String, String> row = new HashMap<String, String>();
        String mKey = e.getKey();
        String mValue = (String) e.getValue();
        row.put(KEY, mKey);
        row.put(VALUE, mValue);
        list.add(row);

        // FOR DEBUGGING PURPOSES
        makeToast(mKey);
        makeToast(mValue);
    }

    SimpleAdapter adapter = new SimpleAdapter(
        this,
            list,
            R.layout.list_item,
            new String[] { KEY, VALUE },
            new int[] {R.id.item1, R.id.item2}
    );

    setListAdapter(adapter); 

This sort of works - but only half way... what I get as a result is a list of the VALUES in both columns...

HOWEVER the makeToast function returns the proper values for both the KEY and the VALUE - so the problem must be in the SimpleAdapter method yes??

assistance would be great - homework is due tonight! :-0

Was it helpful?

Solution

You need to search for "custom listview", "listview custom adapter" and those things. "two line listview item layout"...

See this example. There are others on Google.

Basically, you can create a ArrayList<Hashmap<String,String>>, which is your data container. You add values to that creating as many HashMap<String, String> objects as you need and using list.add(yourHashMap), where list is the ArrayList.

At the end you feed that to a SimpleAdapter (there are other methods, but this works without much trouble).

Check the docs to see how each thing works exactly.


You are nulling your index keys. Put a name into those final Strings.

This sort of works - but only half way... what I get as a result is a list of the VALUES in both columns... HOWEVER the makeToast function returns the proper values for both the KEY and the VALUE - so the problem must be in the SimpleAdapter method yes??

As I said, no. When you do this:

row.put(KEY, mKey);
row.put(VALUE, mValue);

You are not providing a meaninful difference between KEY and VALUE, because both are null. It's something like putting all things into the same column.

Your mistake into reasoning that is because the Toast test you created yourself test only the correctness of the values, not the columns:

makeToast(mKey);
makeToast(mValue);

In that you test only the values. ;) You assume that the columns are right, and that the mistake could only be in the values, which is exactly the opposite.

First rule of dealing with computers: computers never assume LOL. ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top