0

我想要做的是:安卓:如何設置sharedpreferences默認的ListView?

當我的應用程序加載的第一次,我希望它與具有名爲「新建項目」內1個項目我的默認的ListView加載,以後如果更改將進行到我的ListView,我想有一個名爲「默認設置」的按鈕,將ListView返回爲「1項目名爲」New Item「,

從我聽說我需要使用Shared Preferences,但我不知道從哪裏開始。

感謝您的幫助。

+0

http://myandroidsolutions.blogspot.be/2012/03/android-preferenceactivity.html –

回答

0

你的問題是非常模糊。你可能會使用SharedPreferences,但它只是一個存儲機制。

我不建議使用列表上的復位按鈕,但如果你想我會與它一起去。

首先你需要創建一個ArrayAdapter從SharedPreferences加載數據的變化時的感覺。有很多教程。

在您的ListView使用addHeaderView()和addFooterView(),以添加「+新建項目」行,否則會與你的適配器玩一下。基本上只是在添加新項目或刪除列表時通知ArrayAdapter。

如果您需要更多的細節,有很多詳細的教程,如果你谷歌爲它:)

0

您明確要求的問題並不需要SharedPreferences,它所需要的是在你的ListView控件的自定義的方法Adapter

public class CustomAdapter extends BaseAdapter { 

    private ArrayList<String> content; 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView tv = (TextView) convertView.findViewById(R.id.textView1); 
     tv.setText(content.get(position)); 
     return convertView; 
    } 

    private void revert() { 
     ArrayList<String> content = new ArrayList<String>(); 
     content.add("New Item"); 
     this.content = content; 
     notifyDataSetChanged(); 
    } 

然後在您的按鈕的onClick方法,只需調用此方法還原。