私はAndroidが苦手で、物事を学んでいます。Android で SQLite からレコードを取得しようとしています。これが私のコードです。私のせいがどこにあるのか助けてください。ありがとう。
//DatabaseAdapter.java//
package com.example.wg_an;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseAdapter {
SQLiteDatabase database;
DatabaseOpenHelper dbHelper;
public DatabaseAdapter(Context context) {
dbHelper = new DatabaseOpenHelper(context);
}
public void open() {
database = dbHelper.getWritableDatabase();
}
public void close() {
database.close();
}
public long insertTest(String no, String name) {
ContentValues values = new ContentValues();
values.put("no", no);
values.put("name", name);
return database.insert("test", null, values);
}
public ArrayList<String> getAllLabels() {
Cursor mcursor = database.rawQuery("SELECT name FROM "
+ DatabaseOpenHelper.TABLE_NAME, null);
ArrayList<String> result = new ArrayList<String>();
do {
result.add(mcursor.getString(mcursor.getColumnIndex("name")));
} while (mcursor.moveToNext());
return result;
}
}
//MainActivity2.java//
package com.example.wg_an;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity2 extends Activity implements OnClickListener {
EditText etno, etname;
Button btnSave;
DatabaseAdapter dbAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dbAdapter = new DatabaseAdapter(getApplicationContext());
etno = (EditText) findViewById(R.id.txtNo);
etname = (EditText) findViewById(R.id.txtName);
btnSave = (Button) findViewById(R.id.Save);
btnSave.setOnClickListener(this);
Button back = (Button) findViewById(R.id.buttonBackPg);
back.setOnClickListener(this);
Button view_list = (Button) findViewById(R.id.view_list);
view_list.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.buttonBackPg) {
startActivity(new Intent(getApplicationContext(),
MainActivity.class));
}
if (v.getId() == R.id.view_list) {
ListView listview = (ListView) findViewById(R.id.list_all);
ArrayList<String> data = dbAdapter.getAllLabels();
dbAdapter.close();
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
if (v.getId() == R.id.Save) {
String no = etno.getText().toString();
String name = etname.getText().toString();
dbAdapter.open();
long inserted = dbAdapter.insertTest(no, name);
if (inserted >= 0) {
Toast.makeText(getApplicationContext(), "data saved",
Toast.LENGTH_LONG).show();
etno.setText("");
etname.setText("");
} else {
Toast.makeText(getApplicationContext(), "data not saved",
Toast.LENGTH_LONG).show();
}
dbAdapter.close();
}
}
}
//activity_main2.xml//
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity2" >
<EditText
android:id="@+id/txtNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:ems="10"
android:hint="No"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/buttonBackPg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Save"
android:layout_alignParentBottom="true"
android:layout_marginBottom="77dp"
android:text="Back" />
<Button
android:id="@+id/Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/buttonBackPg"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="onClick"
android:text="Save" />
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/txtNo"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Name"
android:inputType="text">
</EditText>
<Button
android:id="@+id/view_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtName"
android:layout_below="@+id/Save"
android:text="View List" />
<ListView
android:id="@+id/list_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/msg"
android:layout_alignLeft="@+id/msg" >
</ListView>
</RelativeLayout>