package com.ctis.abc_final;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayActivity extends Activity {
DBAdapter db;
TextView t1;
TextView t2;
TextView t3;
TextView t4;
TextView t5;
TextView t6;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.display);
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/Ctis487";
File file = new File(destPath);
if (!file.exists()) {
CopyDB(getBaseContext().getAssets().open("Ctis487"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
t1 = (TextView) findViewById(R.id.textView8);
t2 = (TextView) findViewById(R.id.textView9);
t3 = (TextView) findViewById(R.id.textView10);
t4 = (TextView) findViewById(R.id.textView11);
t5 = (TextView) findViewById(R.id.textView12);
t6 = (TextView) findViewById(R.id.textView13);
db = new DBAdapter(this);
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
// Copy 1K bytes at a time
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
// Get all contacts
db.open();
Cursor c = db.getAllContacts();
if (c.moveToFirst()) {
do {
DisplayContact(c);
} while (c.moveToNext());
}
db.close();
break;
}
}
public void DisplayContact(Cursor c) {
t1.setText("" + c.getString(3));
t2.setText("" + c.getString(1));
t3.setText("" + c.getString(2));
t4.setText("" + c.getString(4));
t5.setText("" + c.getString(5));
t6.setText("" + c.getString(6));
}
}
上記のコードは最後の値のみを取得します。私がやりたいことは、次のボタンをクリックすると、次のレコードが取得されることです。どうやってやるの?上記のコードをクリックして定義しました。しかし、最後のレコードしか取得しないのはなぜですか?
これは最初のレコードのみを取得します。UPDATE それを見てください。
switch (v.getId()) {
case R.id.Button01:
int record_position = -1;
// Get all contacts
db.open();
Cursor c = db.getAllContacts();
if (record_position == -1){
if (c.moveToFirst()) {
record_position = c.getPosition();
DisplayContact(c);
}
}
else if (c.moveToPosition(record_position)){
c.moveToNext();
record_position = c.getPosition();
DisplayContact(c);
}
else {
if(c.moveToFirst()){
record_position = c.getPosition();
DisplayContact(c);
}
}
db.close();
break;