2016-06-20 7 views
1

こんにちは。誰も効率的な方法でリストビューの配列アイテムをテキストファイルにロードします。 このコードに保存とロードをどのように統合しますか?Efficientwayを保存する/リストアイテムをテキストファイルに読み込む

テキストに保存するなどの指示があります。

  File f = File.createTempFile("file", ".txt",Environment.getExternalStorageDirectory()); 
     FileWriter fw = new FileWriter(f); 
     fw.write(m_listItems); 
     fw.close(); 

私がしたいことは、リストビューに追加するリストアイテムを保存して読み込む機能を持たせることです。どんな助けでも大歓迎です。道に迷いました。

あらかじめおねがいします。

public class ListtestActivity extends Activity { 
ArrayAdapter<String> m_adapter; 
ArrayList<String> m_listItems = new ArrayList<String>(); 


Button bt; 
Button save; 
Button Loadtxt; 
EditText et; 
TextView tv; 
ListView lv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
save = (Button) findViewById(R.id.button2); 
Loadtxt = (Button) findViewById(R.id.button3); 
bt = (Button) findViewById(R.id.button1); 
et = (EditText) findViewById(R.id.editText1); 
tv = (TextView) findViewById(R.id.textView1); 
lv = (ListView) findViewById(R.id.listView1); 
m_adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, m_listItems); 

lv.setAdapter(m_adapter); 
final String input = et.getText().toString(); 

bt.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 

    String input = et.getText().toString(); 
    if(null!=input&&input.length()>0){  

    m_listItems.add(input); 

    m_adapter.notifyDataSetChanged(); 

    } 

save.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 



    } 

Loadtxt.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 



} 
} 
}); 
+0

ファイルではなくデータベースに保存できます。 [Android用の領域]を使用する(https://realm.io/) – KDeogharkar

答えて

0

これを試してみてください、あなたが速いと堅牢なソリューションをしたい場合は、

public void writeToFile(String fileName) 
{ 
    try { 
     final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/folderName/"); 

     if (!dir.exists()) 
     { 
      if(!dir.mkdirs()){ 
       Log.e("TAG","could not create the directories"); 
      } 
     } 

     final File myFile = new File(dir, fileName + ".txt"); 
     if (!myFile.exists()) 
      myFile.createNewFile(); 



     FileWriter writer = new FileWriter(myFile); 
     for(String str: m_listItems) { 
      writer.write(str); 
     } 
     writer.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
0

、あなたはJavaオブジェクトの直列化を使用することができますするのに役立つかもしれません。しかし、バイナリ形式で保存されるため、あなたが書いたファイルは人間が読めるようにはなりません。

http://www.vogella.com/tutorials/JavaSerialization/article.html

今日あなたがArrayListの<文字列>を使用している、ので、私は堅牢言います。明日、ArrayList <MyOwnClass>に変更したい場合は、変更を行わずにシリアル化オプションを使用して、オブジェクトの保存と読み込みを行うことができます。

シリアライゼーションのような堅牢な使い方を人間が読めるようにしたい場合は、GsonのようなJSONライブラリを使用して内容を直接ファイルにストリーミングすることができます。

https://stackoverflow.com/a/29319491/1364747

0

Simple XML Frameworkは、XML形式で保存ファイルにあなたのオブジェクトをシリアル化することが非常に容易になります。

ライブラリApache CSVライブラリを使用すると、カンマ区切りまたはタブ区切りファイルとしてデータを読み書きすることが容易になります。

検索のオーバーフローは、より多くのディスカッションとその両方の例です。

0

簡単な方法は、オブジェクトのリストを文字列に変換し、必要に応じてデシリアライズすることです。あなたはGsonでこのようにそれを行うことができます。

public class MyObject { 

    @SerializedName("field1") 
    @Expose 
    private String filed1; 
    @SerializedName("field2") 
    @Expose 
    private String field1; 

} 

は、文字列に

String objectsStr = ...//get form file or prefs 
Gson gson = new Gson(); 
List<MyObjects> list = new ArrayList<MyObjects>(); 
list = Arrays.asList(gson.fromJson(objectsStr, MyObjects[].class)); 

は、あなたのクラスでserialisableフィールドをマークすることを忘れてはいけないオブジェクトへ

Gson gson = new Gson(); 
List<MyObject> objects = new ArrayList<>(); 
objects.add(...) //whatever, all your datas to save 

String objectsStr = gson.toJson(objects)); 
//Save you string in a file or in a preference. 

文字列を変換します

0
protected Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException { 
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 
    ObjectInputStream ois = new ObjectInputStream(bais); 
    Object ret= ois.readObject(); 
    ois.close(); 
    return ret; 
} 

protected byte[] objectToBytes(Serializable object) throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ObjectOutputStream oos = new ObjectOutputStream(baos); 
    oos.writeObject(object); 
    oos.close(); 
    return baos.toByteArray(); 
} 
関連する問題