2017-12-26 20 views
1

私は単純な名前を保存して読み込みしようとしています。すべてを自分自身に明瞭にするためにエラーはありませんが、読み込み時に別のものが読み込まれます。それはAndroid自体と関係があります。ここでJSONエラー読み込み

は、私が保存クラスおよびロード(私は任意のライブラリを使用していない)である:ここでは

public class Serializer 
{ 
private Context context; 
private String fn; 
public Serializer(Context con , String filename){ 
    this.context = con; 
    this.fn = filename; 
} 
public void save(ArrayList<String> usernames) throws JSONException, IOException { 
    JSONArray JsonArray = new JSONArray(); 
    JSONObject obj = new JSONObject(); 
    for(String s : usernames){ 
     obj.put("username" , s); 
     JsonArray.put(obj); 
    } 
    Writer writer = null; 
    OutputStream out = context.openFileOutput(fn , 0); 
    writer = new OutputStreamWriter(out); 
    writer.write(JsonArray.toString()); 
    if(writer != null){ 
     writer.close(); 
    } 
} 
public ArrayList<String> load() throws IOException, JSONException { 
    ArrayList<String > strings = new ArrayList<>(); 
    InputStream in = context.openFileInput(fn); 
    InputStreamReader reader = new InputStreamReader(in); 
    BufferedReader Reader = new BufferedReader(reader); 
    StringBuilder builder = new StringBuilder(); 
    String Line; 
    while((Line = Reader.readLine()) != null){ 
     builder.append(Line); 
    } 
    JSONArray array = (JSONArray)new JSONTokener(builder.toString()).nextValue(); 
    for(int i = 0 ; i < array.length() ; i++){ 
     String jack = (String) array.getJSONObject(i).get("username"); 
     strings.add(jack); 
    } 
    if(reader!=null){ 
     reader.close(); 
    } 
    return strings; 
} 
} 

は、そのレイアウト1つのテキストエディットとシンプルなレイアウトである私のメインの活動です:

public class MainActivity extends AppCompatActivity { 
Serializer serializer; 
ArrayList<String> usernames; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    serializer = new Serializer(this,"Jacop"); 
    TextView txtView = findViewById(R.id.txt); 
    usernames = new ArrayList<>(); 
    usernames.add(txtView.toString()); 
    try { 
     ArrayList<String> username = serializer.load(); 
     txtView.setText(username.get(0)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
@Override 
public void onResume(){ 
    super.onResume(); 
    try { 
     serializer.save(usernames); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

出力(例えば、アプリケーションを閉じて開いた後にテキストビューに表示される内容)。

android.support.v7.widget.AppCompatEditText {73d0e9d VFED..CL。 ......私。 0,0-0,0#7f07007b app:id/txt}

+0

ここで配列を使用するのは完璧ではないことがわかっていますが、将来私は訓練したいので、それを使用する必要があるからです。 –

答えて

1

文字列としてObjectを追加しています。

usernames.add(txtView.toString()); 

各クラスはそれを持っているので、

usernames.add(txtView.getText().toString()); 

toString()に変更し、それは、オブジェクトクラスのメソッドです。 android.support.v7.widget.AppCompatEditText{73d0e9d VFED..CL. ......I. 0,0-0,0 #7f07007b app:id/txt}TextViewオブジェクトの文字列表現です。

+0

ああありがとう –

関連する問題