Android ScrollView

索引サイト




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:onClick="onClickSetDB"
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="SetDB" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</ScrollView>
</LinearLayout>




import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//import android.database.sqlite.SQLiteStatement;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

public class Project3000Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickSetDB(View view){
v_localDB helper = new v_localDB(this);
SQLiteDatabase w_db = helper.getReadableDatabase();

Cursor w_localTBL = w_db.query("products", new String[] { "name", "price" },null, null, null, null, null);
boolean isEof = w_localTBL.moveToFirst();

TextView textView1 = (TextView)findViewById(R.id.textView1);
String w_text="";
while (isEof) {
w_text += String.format("%s : %d円\r\n", w_localTBL.getString(0), w_localTBL.getInt(1));

isEof = w_localTBL.moveToNext();
}
textView1.setText(w_text);
w_localTBL.close();
w_db.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
public class v_localDB extends SQLiteOpenHelper {
public v_localDB(Context context) {
super(context, null, null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// table create
db.execSQL("create table products(" + " name text not null," + " price text" + ");");

db.execSQL("insert into products(name,price) values ('data-11', 110);");
db.execSQL("insert into products(name,price) values ('data-12', 120);");
db.execSQL("insert into products(name,price) values ('data-13', 130);");
db.execSQL("insert into products(name,price) values ('data-14', 140);");
db.execSQL("insert into products(name,price) values ('data-15', 150);");
.
.
.
.
db.execSQL("insert into products(name,price) values ('data-48', 480);");
db.execSQL("insert into products(name,price) values ('data-49', 490);");
db.execSQL("insert into products(name,price) values ('data-50', 500);");
}
}
}

(参考)http://www.ipentec.com/document/document.aspx?page=android-use-sqlite-simple-app




AX