2

这是我正在尝试做的事情: - 从 SharedPreferences 对象(用户#,名称)中检索键值对映射 - 将这些键值对写入 ArrayList 以便我可以 - 使用它们填充 ListView每行都包含键和值,如下所示:

  1. 用户 1 - 乔 R。

  2. 用户 2 - Frank B.

ETC


更新:所以在对 SimpleAdapter 类进行了长时间的研究,并与一些更聪明、更有知识的人交谈之后——我比以前更接近了……但仍然没有走到那一步。

这就是我现在所拥有的:

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); 

这种工作 - 但只有一半......结果我得到的是列中的 VALUES 列表......

但是 makeToast 函数为 KEY 和 VALUE 返回了正确的值 - 所以问题一定出在 SimpleAdapter 方法中是吗?

帮助会很棒 - 今晚要交作业!:-0

4

1 回答 1

1

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. ;-)

于 2011-11-15T20:46:34.550 回答