我有以下代码将名为savedlist
的变量列表保存到sharedprefs
。我一直在尝试保存值(应该是URL列表)以在ListView(lv)中显示。
public class saveSearch extends Activity {
private String urlString = "www.google.com";
private String saveURL;
private final static String PREFERENCE_FILENAME = "TestSave";
private EditText savename;
private ArrayList<String> savelist;
private ArrayList savelist2;
List<String> SavedSearches = new ArrayList<String>();
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String value = intent.getStringExtra("searchurl");
urlString = value;
setContentView(R.layout.save_search);
final SharedPreferences.Editor editor = getSharedPreferences(PREFERENCE_FILENAME,0).edit();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
savename = (EditText) findViewById(R.id.et_savename);
lv = (ListView) findViewById(R.id.listView);
// Get the current list.
final Set<String> myStrings = prefs.getStringSet("savelist", new HashSet<String>());
//Convert Your Set to List again
final ArrayList<String> savelist = new ArrayList<String>(myStrings);
// populate the listview
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
savelist);
lv.setAdapter(arrayAdapter);
final Button browsebutton = (Button) findViewById(R.id.button_Save);
browsebutton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
saveURL = (savename.getText() + " - " + urlString);
// Add the new value.
myStrings.add(saveURL);
// Save the list.
editor.putStringSet("savelist", myStrings);
editor.commit();
arrayAdapter.notifyDataSetChanged();
lv.setAdapter(arrayAdapter);
}})
答案 0 :(得分:0)
这里有一些问题。我们一个接一个地走吧:
lv.setAdapter(arrayAdapter);
。