且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保存和检索的Andr​​oid ListItems的

更新时间:2023-02-02 19:30:50

如果我要救一个列表来共享preferences我总是选择做到以下几点,你会需要GSON库对于这一点,虽然。

首先列表转换为一个JSON字符串:

 键入listOfObjects =新TypeToken<名单,LT; YourObject>方式>(){}的getType();
JSON字符串= gson.toJson(列表,listOfObjects);

然后保存字符​​串到preferences。转换的JSON返回列表执行以下操作:

 键入listOfObjects =新TypeToken<名单,LT; YourObject>方式>(){}的getType();
清单< YourObject>清单= gson.fromJson(JSON,listOfObjects);

How to save state of list items so that i can retrieve it back on restart of phone. Currently in below code you can see i have a listview which has a list items. Now I want to save the state of "items" during onDestroy and onPause and retrieve back during onCreate (Reason for doing this is to retrieve state of data when phone is restarted). Can someone help with code.

public class LogListView extends ListActivity {
    /** Called when the activity is first created. */
    private static String newString;
    private static EntryAdapter adapter;
    int clickCounter = 0;
    static ArrayList<Item> items = new ArrayList<Item>();
    static SharedPreferences preferences = null;
    private static Context context = null;
    static StringTokenizer tokens;
    private static String first;
    private static String second;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;
        adapter = new EntryAdapter(this, items);
        // items.add(new SectionItem("Log Report"));
        setListAdapter(adapter);

        if (adapter.getCount() != 0) {
            // Do nothing Adapter has value
        } else {
            Toast.makeText(LogListView.this, "No Items Available",Toast.LENGTH_SHORT).show();
            //Retrive Preference here when Adapter is null

        }

    }

    // Method which will handle dynamic insertion
    public static void addItems() {

        preferences = context.getSharedPreferences("LOG",android.content.Context.MODE_PRIVATE);
        newString = preferences.getString("log", "");

        tokens = new StringTokenizer(newString, ",");
        first = tokens.nextToken();
        second = tokens.nextToken();

        items.add(new EntryItem(first, second));
        adapter.notifyDataSetChanged();

    }
    // Method which will handle dynamic insertion ends

    @Override
    public void onDestroy() {
        super.onDestroy();

        //Save preference here


    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        if (!items.get(position).isSection()) {

            items.get(position);
            // Toast.makeText(this, "You clicked " + item.title ,
            // Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "You clicked " + position, Toast.LENGTH_SHORT)
                    .show();

        }

        if (position == 9) {

        }

        super.onListItemClick(l, v, position, id);
    }
}

If I have to save a List to SharedPreferences I always chose to do the following, you're going to need the Gson library for this, though.

First convert the list to a JSON String:

Type listOfObjects = new TypeToken<List<YourObject>>(){}.getType();
String json = gson.toJson(list, listOfObjects);

Then save the String to the preferences. To convert the json back to the list do the following:

Type listOfObjects = new TypeToken<List<YourObject>>(){}.getType();
List<YourObject> list = gson.fromJson(json, listOfObjects);