2016-11-22 49 views
0

我是新来的Android和Java,但我一直在努力,现在一会做到这一点,有一些想法,这可能如何制定一个ListView,但我不知道如何把它的代码。填充用的密钥集

我有两个活动,一个是一个日志,一个创建一个新的日志。基本上,当一个用户创建一个新日志并保存它时,它将被保存为一个密钥集,密钥是日志名称。

然后我想要将新创建的日志条目添加到另一个活动的列表中。

我只能似乎所有的按键组,而不是他们来填充列表中的第一项被添加为新的项目逐一列出。

我需要的代码只是为了在ListView每个项目填充作为的KeySet的关键之一,当用户点击它加载的值。

的代码,我从ActivityLog有:

public void loadLog (View view){ 

SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = userInfo.edit(); 
    String myString = userInfo.getAll().keySet().toString(); 
    String[] values = new String[] { myString 
    }; 


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text1); 


    Map<String, ?> allEntries = userInfo.getAll(); 
    for (Map.Entry<String, ?> entry : allEntries.entrySet()) { 

     Log.d("map values", entry.getKey() + ": " + entry.getValue().toString()); 
     listView.setAdapter(adapter); 

    }} 

代码ActivityNewLog:

public void saveLog (View view){ 


    EditText Date = (EditText)findViewById(R.id.editDate); 
    EditText Name = (EditText)findViewById(R.id.editName); 
    EditText Cal = (EditText)findViewById(R.id.editCal); 
    EditText STime = (EditText)findViewById(R.id.editSTime); 
    EditText ETime = (EditText)findViewById(R.id.editETime); 
    EditText Entry = (EditText)findViewById(R.id.editEntry); 

    try { 
     SharedPreferences userInfo = getSharedPreferences("userData", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = userInfo.edit(); 
     Set<String> LogSet = new HashSet<String>(); 

     LogSet.add(Date.getText().toString()); 
     LogSet.add(Name.getText().toString()); 
     LogSet.add(Cal.getText().toString()); 
     LogSet.add(STime.getText().toString()); 
     LogSet.add(ETime.getText().toString()); 
     editor.putStringSet(Entry.getText().toString(), LogSet); 
     editor.commit(); 
     Context context = getApplicationContext(); 
     CharSequence text = "User data saved!"; 
     int duration = Toast.LENGTH_LONG; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 


    } 
    catch(Exception ex) { 
     // insert your own error message here 
    } 


} 

我将不胜感激任何帮助或建议,以改善该代码。

+0

为什么你需要'Set'而不是'List'? –

+0

我使用了集合,因为每个集合都保存新日志屏幕的值,例如日期,名称,时间等,我需要稍后访问它们,并且我只是假定集合是最佳选项。 –

回答

0

我会建议你使用SQLite数据库,而不是在这里你把一个关键到SharedPreferences/HashMap

editor.putStringSet(Entry.getText().toString(), LogSet); 

现在,你必须得到该值回来了......

Map<String, ?> allEntries = userInfo.getAll(); 
Set<String> entry = allEntries.get("<some value here>"); // <-- Whatever you put in 

而当你有,你可以遍历他们添加到适配器。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, android.R.id.text1); 
listView.setAdapter(adapter); 

for (String s : entry) { 
    adapter.add(s); 
} 
+0

发布后这个我用了一个非常类似于此方法,但以不同的方式结构化的方法,它的工作原理非常感谢,因为这是完美的 –

+0

很高兴听到。您可以通过[接受答案](http://stackoverflow.com/help/someone-answers)显示您的感谢,或发布您自己的解决方案 –