android - How to insert data from edittext -
i try insert db.insert(inputfname, inputlname, inputnickname); it's error .it tell me "the method insertdata(edittext, edittext, edittext) undefined type friendsdb insertactivity.java" can tell me please
this code (mainactivity) package 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 } } } );
this friendsdb code
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 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; }
change this
db.insert(inputfname, inputlname, inputnickname);
to
db.insert(inputfname.gettext().tostring(), inputlname.gettext().tostring(), inputnickname.gettext().tostring());
your database insert method takes strings arguments.
insert(string fname,string lname,string nickname)// arguments strings
Comments
Post a Comment