简体   繁体   English

Android Shared Prefs将键值映射到ArrayList到ListView行-更新

[英]Android Shared Prefs Map key-values to ArrayList to ListView row - UPDATED

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: 这是我要执行的操作:-从SharedPreferences对象(用户#,名称)中检索键-值对的映射-将这些键-值对写入ArrayList中,以便我可以-用它们来填充ListView每行都包含键和值,如下所示:

  1. User 1 - Joe R. 用户1-JoeR。

  2. User 2 - Frank B. 用户2-FrankB。

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. 更新:因此,在仔细看了一下SimpleAdapter类并与一些更睿智的,知识渊博的人交谈之后-我比我离得更近了……但仍然没有到此为止。

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?? 但是,makeToast函数同时为KEY和VALUE返回正确的值 -因此问题一定出在SimpleAdapter方法中吗?

assistance would be great - homework is due tonight! 援助将非常棒-作业应在今晚完成! :-0 :-0

You need to search for "custom listview", "listview custom adapter" and those things. 您需要搜索“自定义列表视图”,“列表视图自定义适配器”以及这些内容。 "two line listview item layout"... “两行listview项的布局” ...

See this example . 请参阅此示例 There are others on Google. Google上还有其他人。

Basically, you can create a ArrayList<Hashmap<String,String>> , which is your data container. 基本上,您可以创建一个ArrayList<Hashmap<String,String>> ,它是您的数据容器。 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. 您可以根据需要添加值,以创建所需的HashMap<String, String>对象,并使用list.add(yourHashMap) ,其中list是ArrayList。

At the end you feed that to a SimpleAdapter (there are other methods, but this works without much trouble). 最后,将其提供给SimpleAdapter (还有其他方法,但这可以正常工作)。

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?? 这种工作-但只有一半...结果我得到的是两列中的值列表...但是makeToast函数同时返回键和值的正确值-因此问题必须是在SimpleAdapter方法中吗?

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. 您没有在KEYVALUE之间提供明显的区别,因为两者都是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: 您在推理中的错误是因为您创建的Toast测试仅测试值的正确性,而不测试列的正确性:

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. 处理计算机的第一条规则:计算机从不承担LOL。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM