android - Applying on item click listener on item in the listview -
this code of database helper class can please tell me doing wrong email column not being added in table , each time getting error table has no cloumn name person_email
package com.example.and;
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class androidopendbhelper { private static final string tag = androidopendbhelper.class.getsimplename(); // database configuration // if want onupgrade run change database_version private static final int database_version = 1; private static final string database_name = "mydatabase.db"; // table configuration private static final string table_name = "person_table"; // table name private static final string person_table_column_id = "_id"; // column named "_id" required cursor private static final string person_table_column_name = "person_name"; private static final string person_table_column_number = "person_contact_number"; private static final string person_table_column_email = "person_email"; private databaseopenhelper openhelper; private sqlitedatabase database; // wrapper class. means, outside world, communicate persondatabasehelper, // under hood databaseopenhelper class perform database crud operations public androidopendbhelper(context acontext) { openhelper = new databaseopenhelper(acontext); database = openhelper.getwritabledatabase(); } public void insertdata (string apersonname, string apersoncontactnumber, string apersonemail) { // using contentvalues avoid sql format errors contentvalues contentvalues = new contentvalues(); contentvalues.put(person_table_column_name, apersonname); contentvalues.put(person_table_column_number, apersoncontactnumber); contentvalues.put(person_table_column_email, apersonemail); database.insert(table_name, null, contentvalues); } public cursor getalldata () { string buildsql = "select * " + table_name; log.d(tag, "getalldata sql: " + buildsql); return database.rawquery(buildsql, null); } // databaseopenhelper class used perform database related operation private class databaseopenhelper extends sqliteopenhelper { public databaseopenhelper(context acontext) { super(acontext, database_name, null, database_version); } @override public void oncreate(sqlitedatabase sqlitedatabase) { // create tables here string buildsql = "create table " + table_name + "( " + person_table_column_id + " integer primary key autoincrement, " + person_table_column_name + " text, " + person_table_column_number + " text " +")" ; //+ person_table_column_email + "text )"; log.d(tag, "oncreate sql: " + buildsql); sqlitedatabase.execsql(buildsql); } @override public void onupgrade(sqlitedatabase sqlitedatabase, int oldversion, int newversion) { // database schema upgrade code goes here string buildsql = "drop table if exists " + table_name; log.d(tag, "onupgrade sql: " + buildsql); sqlitedatabase.execsql(buildsql); // drop previous table oncreate(sqlitedatabase); // create table beginning } } }
this mainactivity class
package com.example.and; import android.app.activity; import android.content.clipdata.item; import android.content.intent; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; public class mainactivity extends activity { private datacursoradapter customadapter; private androidopendbhelper databasehelper; private static final int enter_data_request_code = 1; private listview listview; private static final string tag = mainactivity.class.getsimplename(); /** * called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); databasehelper = new androidopendbhelper(this); listview = (listview) findviewbyid(r.id.list_data); listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(mainactivity.this, addnewundergraduateactivity.class); // intent.putextra("number",itemclicked ); startactivity(intent); //log.d(tag, "clicked on item: " + position); } }); // database query can time consuming task .. // safe call database query in thread // handler, handle stuff <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley"> new handler().post(new runnable() { @override public void run() { customadapter = new datacursoradapter(mainactivity.this, databasehelper.getalldata()); listview.setadapter(customadapter); } }); } public void onclickenterdata(view btnadd) { startactivityforresult(new intent(this, addnewundergraduateactivity.class), enter_data_request_code); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == enter_data_request_code && resultcode == result_ok) { databasehelper.insertdata(data.getextras().getstring("tag_person_name"), data.getextras().getstring("tag_person_contact_number"), data.getextras().getstring("tag_person_email") ); customadapter.changecursor(databasehelper.getalldata()); } }
}
now want know how pass item selected other activity , fetch data data according selected item. since list item customized cursor object.so, cant recognize how it.
this cursor adapter
package com.example.and; import android.content.context; import android.database.cursor; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.cursoradapter; import android.widget.textview; public class datacursoradapter extends cursoradapter { public datacursoradapter(context context, cursor c) { super(context, c); } @override public view newview(context context, cursor cursor, viewgroup parent) { // when view created first time, // need tell adapters, how each item layoutinflater inflater = layoutinflater.from(parent.getcontext()); view retview = inflater.inflate(r.layout.listcell, parent, false); return retview; } @override public void bindview(view view, context context, cursor cursor) { // here setting our data // means, take data cursor , put in views textview textviewpersonname = (textview) view.findviewbyid(r.id.tv_person_name); textviewpersonname.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(1)))); textview textviewpersonpin = (textview) view.findviewbyid(r.id.tv_person_pin); textviewpersonpin.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(2)))); textview textviewpersonemail = (textview) view.findviewbyid(r.id.tv_person_email); textviewpersonpin.settext(cursor.getstring(cursor.getcolumnindex(cursor.getcolumnname(3)))); }
}
remove comment , correct query below:
string buildsql = "create table " + table_name + "(" + person_table_column_id + " integer primary key autoincrement, " + person_table_column_name + " text, " + person_table_column_number + " text," + person_table_column_email + " text)";
and reinstalled app.
Comments
Post a Comment