ファイルからテキストを取得し、それをTextViewで表示したいとします。ファイルからソートする
私は、zapisi()メソッドを使用して、poenibrojanjeという名前の1つの文字列と1つの整数を3.txtというファイルに書き込むクラスを持っています。
public void zapisi() {
// WRITING
String eol = System.getProperty("line.separator");
String a = txtIme.getText().toString();
try {
FileOutputStream fOut = openFileOutput("3.txt", MODE_APPEND);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("Ime: " + a + " Poeni: " + a1.poenibrojanje + eol);
// Log.d("Writing", "This is writing log: " + a +
// +a1.poenibrojanje);
// osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
また、ファイルから読み込んだクラスをTextViewに表示します。そのクラスはRankPrikazActivityと呼ばれ、ここでは完全なコードです。
パッケージcom.test.brzoracunanje;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class RankPrikazActivity extends Activity implements OnClickListener {
TextView tvRank, tvRankPrikaz;
Button btnPovratak;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rankprikaz);
tvRank = (TextView)findViewById(R.id.tvRank);
tvRankPrikaz = (TextView)findViewById(R.id.tvRankPrikaz);
btnPovratak = (Button)findViewById(R.id.btnPovratak);
btnPovratak.setOnClickListener(this);
citaj();
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this,PocetnaActivity.class);
startActivity(intent);
}
public void citaj() {
// READING
String eol = System.getProperty("line.separator");
try {
BufferedReader input = new BufferedReader(new InputStreamReader(
openFileInput("3.txt")));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = input.readLine()) != null) {
buffer.append(line + eol);
}
//Log.d("Reading log", "This is reading log:" + buffer);
//System.out.println(buffer);
tvRankPrikaz.setText(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
このクラスの下citaj()メソッドと呼ばれる方法で、私は、ファイルからそれを読むと、このラインでのTextViewでそれを示しています。
tvRankPrikaz.setText(buffer.toString());
今、私はそれを表示したいですこのテキストボックスにRankActivityクラスの整数poenibrojanjeでソートされます。
どうすればよいですか?
英語の単語に基づいて名前(クラス、フィールド、メソッドなど)を行うことをお勧めします。 – hovanessyan
ええ、私は今なぜ英語の命名をすべきか見ています。とにかく、RankActivityクラスからpoenibrojanje整数でソートする必要があります。そこではファイルに書き込んで読み込み、RankPrikazActiviyクラスでソートして表示します。 – Zookey
ファイルにデータ表現を表示できましたか? –