java - how do i listview certain data only in my table -
i have 2 tables: subject , notes table. want listview notes have same subject name chosen.
first listview of subject(view_subj) when clicked goes listview of notes of subject, , add button inserts notes subject view_subj.java is:
public class view_subj extends listactivity { private final string sample_db_name = "project"; private final string sample_table_name = "subject"; sqlitedatabase sampledb = null; arraylist<string> results = new arraylist<string>(); /** called when activity first created. */ public void onlistitemclick(listview l, view view, final int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, view, position, id); intent in = new intent (view_subj.this,view_ppt_list.class); string subj = (l.getitematposition(position)).tostring(); toast.maketext(getapplicationcontext(), subj, toast.length_short).show(); in.putextra("com.example.tinio_bolasa_project.sub1", subj); startactivity(in); } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); try { sampledb = this.openorcreatedatabase(sample_db_name, mode_private, null); //name, mode mode_world_readable, mode_world_writeable, factory /*sampledb.execsql("create table if not exists " + sample_table_name + " (lastname varchar, firstname varchar," + " city varchar, age int(3));");*/ cursor c = sampledb.rawquery("select * " + sample_table_name, null); if (c != null ) { if (c.movetofirst()) { { string subid = c.getstring(c.getcolumnindex("subj_id")); string subject = c.getstring(c.getcolumnindex("subjname")); results.add(subid + subject); }while (c.movetonext()); } } this.setlistadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1,results)); } catch (sqliteexception se ) { log.e(getclass().getsimplename(), "could not create or open database"); } { if (sampledb != null) //sampledb.execsql("delete " + sample_table_name); sampledb.close(); } } }
my view_ppt_list.java:
public class view_ppt_list extends listactivity { private final string sample_db_name = "project"; private final string ppt_table_name1 = "notes"; private final string ppt_table_name2 = "subject"; sqlitedatabase notesdb = null; arraylist<string> results = new arraylist<string>(); public void onlistitemclick(listview l, view view, final int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, view, position, id); intent ins = new intent (view_ppt_list.this,pptactivity.class); string s = (l.getitematposition(position)).tostring(); ins.putextra("com.example.tinio_bolasa_project.finame", s); // ins.putextra("entries", l.getitematposition(position).tostring()); toast.maketext(getapplicationcontext(),s ,toast.length_short ).show(); startactivity(ins); } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); string name = getsublabel(); try{ notesdb = this.openorcreatedatabase(sample_db_name, mode_private, null); notesdb.execsql("create table if not exists " + ppt_table_name1 + " ( notes_id integer primary key autoincrement, " + "subjlabel varchar, " + "pptname varchar, " + "pptpath varchar);"); // int x1 = getintent().getextras().getint("entries"); cursor c = notesdb.rawquery("select * notes subjlabel='"+name+"'", null); if (c != null ) { if (c.movetofirst()) { { string ppt = c.getstring(c.getcolumnindex("pptname")); results.add(ppt); }while (c.movetonext()); } } this.setlistadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1,results)); } catch (sqliteexception se ) { log.e(getclass().getsimplename(), "could not create or open database"); } { if (notesdb != null) //sampledb.execsql("delete " + sample_table_name); notesdb.close(); } setcontentview(r.layout.activity_view__ppt__list); button addppt = (button) this.findviewbyid(r.id.button1); addppt.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub intent inten = new intent (view_ppt_list.this, add_ppt_activity.class); startactivity(inten); } }); } public string getsublabel(){ string sublabel = getintent().getstringextra("com.example.tinio_bolasa_project.finame"); return sublabel; } }
and add_ppt_activity:
public class add_ppt_activity extends listactivity { private final string sample_db_name = "project"; private final string ppt_table_name1 = "notes"; sqlitedatabase notesdb = null; view_ppt_list getsubla = new view_ppt_list(); arraylist<string> filesinfolder = getfiles(environment.getexternalstoragedirectory() .getpath()); public arraylist<string> getfiles(string directorypath) { arraylist<string> myfiles = new arraylist<string>(); file f = new file(directorypath); f.mkdirs(); file[] files = f.listfiles(); if (files.length == 0) return null; else { for(int i=0; i<files.length;i++) myfiles.add(files[i].getname()); } return myfiles; } public void onlistitemclick(listview l, view view, final int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, view, position, id); string filename = filesinfolder.get(position); string filepath = environment.getexternalstoragedirectory() .getpath() + "/" + filename; string subla = getsubla.getsublabel(); notesdb = this.openorcreatedatabase(sample_db_name, mode_private, null); notesdb.execsql("insert " + ppt_table_name1 + " (subjid, "+ "pptname, " + "pptpath)" + " values ('"+subla+"', '"+filename+"', '"+filepath+"');"); intent in = new intent (add_ppt_activity.this,view_ppt_list.class); startactivity(in); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setlistadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1,filesinfolder)); } }
please help,
Comments
Post a Comment