Saturday 15 March 2014

Android Custom XML layout for SQLite -



Android Custom XML layout for SQLite -

this question has reply here:

custom listview android 1 reply

so have been working on project app reads database , prints contents of db list view. want print more 1 column, want add together 2 buttons , edittext row beingness printed. have created new xml file want utilize when seek set new layout xml file doesnt work. can explain or show me how prepare problem?

this xml layout want utilize each row

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <textview android:id="@+id/item1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="30sp" android:paddingbottom="5dp" android:hint="@string/hint"/> <button android:id="@+id/plusbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/plusbutton" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> <edittext android:id="@+id/edit1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/hint" android:layout_alignparenttop="true" android:layout_toleftof="@+id/minusbutton" android:layout_tostartof="@+id/minusbutton" android:layout_marginright="30dp" /> <button android:id="@+id/minusbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/minusbutton" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> </relativelayout>

and class displaying contents of sqlite database, error occurs on line 100 when seek alter android.r.layout row.xml file

package com.example.rory.dbtest; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.arraylist; import android.app.activity; import android.app.listactivity; import android.content.intent; import android.database.cursor; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.baseadapter; import android.widget.button; import android.widget.listview; import android.widget.toast; import com.pinchtapzoom.r; public class myactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); button addbtn = (button)findviewbyid(r.id.add); addbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, addassignment.class); startactivity(i); } }); button deletebtn = (button)findviewbyid(r.id.delete); deletebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, delete.class); startactivity(i); } }); button updatebtn = (button)findviewbyid(r.id.update); updatebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(myactivity.this, update.class); startactivity(i); } }); seek { string destpath = "/data/data/" + getpackagename() + "/databases/assignmentdb"; file f = new file(destpath); if (!f.exists()) { copydb( getbasecontext().getassets().open("mydb"), new fileoutputstream(destpath)); } } grab (filenotfoundexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } com.example.rory.dbtest.dbadapter db = new com.example.rory.dbtest.dbadapter(this); db.open(); arraylist<string> data_list=new arraylist<string>(); listview lv=(listview)findviewbyid(r.id.listview1); cursor c = db.getallrecords(); if (c.movetofirst()) { { data_list.add(c.getstring(2)); //data_list.add(c.getstring(1)); //displayrecord(c); } while (c.movetonext()); } arrayadapter<string> aa=new arrayadapter<string>(getapplicationcontext(), android.r.layout.row, data_list); lv.setadapter(aa); } private class dbadapter extends baseadapter { private layoutinflater minflater; //private arraylist<> @override public int getcount() { homecoming 0; } @override public object getitem(int arg0) { homecoming null; } @override public long getitemid(int arg0) { homecoming 0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { homecoming null; } } public void copydb(inputstream inputstream, outputstream outputstream) throws ioexception { //---copy 1k bytes @ time--- byte[] buffer = new byte[1024]; int length; while ((length = inputstream.read(buffer)) > 0) { outputstream.write(buffer, 0, length); } inputstream.close(); outputstream.close(); } public void displayrecord(cursor c) { toast.maketext(this, "id: " + c.getstring(0) + "\n" + "item: " + c.getstring(1) + "\n" + "litres: " + c.getstring(2), toast.length_short).show(); } }

my new custom adapter class bundle com.example.rory.dbtest;

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; import com.pinchtapzoom.r; /** * created rory on 07/11/2014. */ public class mycustomadapter extends cursoradapter { dbadapter db = new dbadapter(this); private layoutinflater cursorinflater; public mycustomadapter(context context, cursor c) { super(context, c); cursorinflater = (layoutinflater) context.getsystemservice( context.layout_inflater_service); } @override public view newview(context context, cursor cursor, viewgroup viewgroup) { homecoming cursorinflater.inflate(r.layout.row, parent, false); } @override public void bindview(view view, context context, cursor cursor) { textview textviewtitle = (textview) view.findviewbyid(r.id.row); string title = cursor.getstring( cursor.getcolumnindex( database_table.key_item ) ) textviewtitle.settext(title); } }

your question has been asked many times before here. check link instead of using refular arrayadapter, need create custom adapter class , populate view in list items.

android xml sqlite

No comments:

Post a Comment