简体   繁体   English

在Android中离开活动后如何保留我的列表视图?

[英]How do I retain my listview after leaving the activity in android?

I have a listview in which I want to store specific data everytime the user clicks on the add button. 我有一个listview,我想在每次用户点击添加按钮时存储特定数据。 However until now I have had little succes with the implementation. 但是到目前为止,我在实施方面几乎没有成功。 I know that it has to be done by using sharedpreferences and have tried to implement the saving status of sharedpreferences and the restoring status, but still my listview only adds the element that is currently clicked. 我知道必须通过使用sharedpreferences来完成它并尝试实现sharedpreferences的保存状态和恢复状态,但我的listview仍然只添加当前单击的元素。

    package com.example.daniel.pset3_daniel_jacob;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class listview extends AppCompatActivity {

    ArrayList<String> dataa;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        onRestoredPreferences();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        Bundle extras = getIntent().getExtras();
        String dataExtra = extras.getString("key");
        dataa = new ArrayList<String>();
        dataa.add(dataExtra);
        ListView listView = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
        assert listView != null;
        listView.setAdapter(adapter);
        Savedpreferences(dataa);


    }

    public void Savedpreferences(List<String> dataa) {


        SharedPreferences preferences = this.getSharedPreferences("object", 0);
        SharedPreferences.Editor editor = preferences.edit();
        List<String> arraylist = new ArrayList<String>(dataa);
        Set<String> newset = new HashSet<String>(arraylist);
        editor.putStringSet("stringset", newset);
        editor.commit();
    }


    public void onRestoredPreferences()
    {
        SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
        Set<String> getdataback = preferences.getStringSet("stringset", null);
        List <String> arraylist = new ArrayList<String>(getdataback);
    }

}

I hope someone can help me to tell me what i am doing wrong 我希望有人可以帮我告诉我我做错了什么

Try the below code and let me know if any issues. 尝试以下代码,如果有任何问题,请告诉我。

public class ListviewActivity extends AppCompatActivity {

        ArrayList<String> dataa;

        List <String> arraylist = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            onRestoredPreferences();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
            Bundle extras = getIntent().getExtras();
            String dataExtra = extras.getString("key");
            dataa = new ArrayList<String>();
            dataa.addAll(arraylist);
            dataa.add(dataExtra);
            ListView listView = (ListView) findViewById(R.id.listview);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, dataa);
            assert listView != null;
            listView.setAdapter(adapter);
            Savedpreferences(dataa);


        }

        public void Savedpreferences(List<String> dataa) {


            SharedPreferences preferences = this.getSharedPreferences("object", 0);
            SharedPreferences.Editor editor = preferences.edit();
            List<String> arraylist = new ArrayList<String>(dataa);
            Set<String> newset = new HashSet<String>(arraylist);
            editor.putStringSet("stringset", newset);
            editor.commit();
        }


        public void onRestoredPreferences()
        {
            SharedPreferences preferences = getApplicationContext().getSharedPreferences("object", 0);
            Set<String> getdataback = preferences.getStringSet("stringset", null);
            arraylist.clear();
            arraylist.addAll(getdataback);
        }

    }

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

相关问题 带有ArrayList的Android自定义ListView如何打开第二个活动 - Android Custom ListView with ArrayList How do I open a second activity 在“新的Intent(ContactsContract.Intents.Insert.ACTION)”启动的活动完成后,如何刷新ListView? - How do I refresh my ListView after the Activity launched by “new Intent(ContactsContract.Intents.Insert.ACTION)” finishes? 退出Android活动后保留ListView数据 - Retaining ListView data after leaving Android acitivity 从列表视图活动中选择一个选项后,如何返回主活动 - How can I go back to main activity after selecting an option from my listview activity 如何更改活动简历的列表视图? - How do i change a listview on activity resume? 如何在Android的ListView中添加图像? - how do I add an image to my listview in android? 如何在Android Studio中包括对自定义列表视图的搜索? - How do I include a search for my custom listview in android studio? 如何在Android的第二个活动中显示信息 - How do I display information in my second activity in Android Android:如何告诉我的活动从服务中开始新的活动? - Android: how do I tell my activity to start new activity from my service? 如何将单击的 ListView 项目的文本发送到我的新活动? - How do I send the text of the ListView item that was clicked to my new activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM