ANDROID - How I can keep all items from ListView in a Text File, TextView or similar?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:17:31

问题


I have a "Dynamic ListView" which I keep adding elements into by clicking on items in another "Normal ListView".

Later I have one Button, I need to save all the ListView items when I click on this button because later I have to display the items in another activity and I don't know how I can display the Listview directly.

  • ArrayString
  • Text File
  • TextView
  • EditText
  • Somewhere similar

My problem

How I can do that?


回答1:


You could save your list file in the SharedPrefrences as a String, where each item is seperated by a comma.

By saving the list to the SharedPrefrences you can use it even if the app closed since you saved it and then re-opened at another time. But if you don't want this - use the way @Ved Prakash mentioned.

Seperated by a comma way:

ArrayList<String> yourData =...

private void saveList(ArrayList<String> data)
{
String items = "";
for(String s : data)
items+= s+ ",";

SharedPreferences spf = getSharedPrefernces("myappname_data",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = spf.edit();

editor.putString("listview_data",items);
editor.commit();
}

Then to get your data back:

private ArrayList<String> getListData(){
SharedPreferences spf = getSharedPrefernces("myappname_data",Context.MODE_PRIVATE);
String raw_data = spf.getString("listview_data","");

String[] items = raw_data.split(",");

return Arrays.asList(items);
}



回答2:


If it is not permanent you can pass String[] or ArrayList to another Activity:

Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("some_key", new String[] {});
intent.putStringArrayListExtra("some_another_key", new ArrayList<String>());
startActivity(intent);



回答3:


Store the String values you want in SharedPreferences like this.

Concat the String divided by ";" :

String list = "str1;str2;str3";

Then...

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor prefsEditor = prefs.edit();
prefsEditor.putString("YOURKEY", list);
prefsEditor.commit();

Retrive the information in other activity:

String list = prefs.getString("YOURKEY", "");
if(!list.equals(""))
   String[] listArray = list.split(";");



回答4:


You can do in this way---

//your listview defined
final ListView listview = (ListView) findViewById(R.id.listview);

//your listview data, you may have your own data in adapter
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };

//keep the data in arraylist and pass as Serializable
final ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}


//onclick to pass to another activity
prev.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
            Intent intent = new Intent(Current.this, Next.class);
            intent.putExtra("data", new DataWrapper(list));
            startActivity(intent);  
        }
    });

The implemented class that holds the data arrayList -

public class DataWrapper implements Serializable {

       private ArrayList<String> myData;

       public DataWrapper(ArrayList<String> data) {
          this.myData = data;
       }

       public ArrayList<String> getUserData() {
          return this.myData;
       }

    }

To retrieve at another activity-

DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra("data");
ArrayList<String> list = dw.getUserData();


来源:https://stackoverflow.com/questions/24077217/android-how-i-can-keep-all-items-from-listview-in-a-text-file-textview-or-sim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!