db.insert(inputfname, inputlname, inputnickname); で挿入しようとしています。しかし、それはエラーです。「メソッドinsertData(EditText、EditText、EditText)は、タイプFriendsDB insertActivity.javaに対して定義されていません」と教えてください
これは私のコード (Mainactivity) パッケージ com.example.sqlite です。
import java.security.PublicKey;
import com.example.sqlite.db.FriendsDB;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class insertActivity extends Activity {
private Context context;
private FriendsDB db;
EditText inputfname;
EditText inputlname;
EditText inputnickname;
Button save;
Button cancle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.insert_layout);
//view matching
inputfname = (EditText)findViewById(R.id.fname);
inputlname = (EditText)findViewById(R.id.lname);
inputnickname = (EditText)findViewById(R.id.nickname);
save = (Button)findViewById(R.id.save);
cancle = (Button)findViewById(R.id.cancle);
//context
context = this;
db = new FriendsDB(context);
//button event
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent insertData = new Intent(insertActivity.this,FriendsListActivity.class);
insertData.putExtra("fname", inputfname.getText().toString());
insertData.putExtra("lname", inputlname.getText().toString());
insertData.putExtra("nickname", inputnickname.getText().toString());
if(insertData!=null){
db.insert(inputfname, inputlname, inputnickname); <<<<<< ERROR
}
}
} );
これは私のFriendsDBコードです
package com.example.sqlite.db;
import java.util.ArrayList;
import com.example.sqlite.entry.FriendEntry;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//ใช้ในการ inert update delete
public class FriendsDB {
private FriendsDBHelper helper;
private SQLiteDatabase db;
public FriendsDB(Context context){
helper = new FriendsDBHelper(context);
db = helper.getWritableDatabase();
}
//insert
public long insert(String fname,String lname,String nickname){
ContentValues values = new ContentValues();
values.put(FriendsDBHelper.TABLE_KEY_FNAME, fname);
values.put(FriendsDBHelper.TABLE_KEY_LNAME, lname);
values.put(FriendsDBHelper.TABLE_KEY_NICKNAME, nickname);
return db.insert(FriendsDBHelper.TABLE_NAME, null ,values);
}
//select all
public ArrayList<FriendEntry> selectAll(){
ArrayList<FriendEntry> friends = new ArrayList<FriendEntry>();
// cursor คือ ชุดข้อมูล
Cursor cursor = db.rawQuery("SELECT*FROM"+FriendsDBHelper.TABLE_NAME+"WHERE id != ?",new String[]{Integer.toString(0)});
cursor.moveToFirst();
if(cursor.getCount()!=0){
do{
FriendEntry friend = new FriendEntry();
friend.setId(cursor.getInt(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_ID)));
friend.setFname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_FNAME)));
friend.setLname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_LNAME)));
friend.setNickname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_NICKNAME)));
friends.add(friend);
}while(cursor.moveToNext());
}
return friends;
}