im having trouble applying icons respective position on gridview. managed apply different names database still didnt realize how same images.
when run app 3 icons created icon same (the first 1 bank1.png) 3 categories. tried stuff icon.setimageresource(alsc.get(position).geticon()); didnt work because says can due string. apreciate help. thanks
savingscata (activity)
package com.mutesoft.faxpro; import java.util.arraylist; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.view.viewgroup.layoutparams; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import android.widget.textview; import android.widget.toast; import com.mutesoft.savings.savingscat; import com.mutesoft.savings.savingscatdatasource; import com.mutesoft.sqlitedb.mysqlitehelper; public class savingscata extends activity implements adapterview.onitemclicklistener { mysqlitehelper mysqlhelper; savingscatdatasource scdatasource; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mysqlhelper = new mysqlitehelper(this); scdatasource = new savingscatdatasource(mysqlhelper.db); inserenaappdadospredefinidossavingscat(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private void inserenaappdadospredefinidossavingscat() { // todo auto-generated method stub arraylist<savingscat> savingitems = scdatasource.getallsavingscat(); gridview gridview = (gridview) findviewbyid(r.id.grid); gridview.setadapter(new imageadapter(this, savingitems)); gridview.setonitemclicklistener(this); } public class imageadapter extends baseadapter { private context context; private arraylist<savingscat> alsc; public imageadapter(context c, arraylist<savingscat> alsc) { context = c; this.alsc = alsc; } // ---returns number of images--- public int getcount() { return alsc.size(); } // ---returns id of item--- public object getitem(int position) { return position; } public long getitemid(int position) { return position; } // ---returns imageview view--- public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view row = inflater.inflate(r.layout.row, parent, false); textview label = (textview) row.findviewbyid(r.id.image_name); label.settext(alsc.get(position).getname()); imageview icon = (imageview) row.findviewbyid(r.id.album_image); // icon.setimageresource(alsc.get(position).geticon()); layoutparams params = (layoutparams) icon.getlayoutparams(); params.width = 70; icon.setlayoutparams(params); return row; } } @override public void onitemclick(adapterview<?> adapterview, view v, int pos, long id) { // imageview icons = new imageview(this); // icons.setalpha(0); // todo auto-generated method stub toast.maketext(this, "weeeeeeeeeeeeee", toast.length_short).show(); }
mysqlitehelper
package com.mutesoft.sqlitedb; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.widget.toast; import com.mutesoft.savings.savingscat; import com.mutesoft.savings.savingscatdatasource; public class mysqlitehelper extends sqliteopenhelper { public static final string database_name = "savings.db"; public static final int database_version = 1; // savings public static final string table_savingscat = "savingscat"; public static final string col_savingscat_id = "_id"; public static final string col_savingscat_name = "name"; public static final string col_savingscat_icon = "icon"; public sqlitedatabase db; context context; public mysqlitehelper(context context) { super(context, database_name, null, database_version); this.context = context; db = getwritabledatabase(); } @override public void oncreate(sqlitedatabase db) { // savings string createsavingscat = "create table " + table_savingscat + " ( " + col_savingscat_id + " integer primary key autoincrement, " + col_savingscat_name + " text, " + col_savingscat_icon + " text);"; db.execsql(createsavingscat); // função para criar icons e secções pre-definidas para savings criadadospredefinidossavingscat(db); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } public void criadadospredefinidossavingscat(sqlitedatabase db) { savingscat sc1 = new savingscat(); sc1.setname("banks"); sc1.seticon("bank1.png"); savingscat sc2 = new savingscat(); sc2.setname("piggy banks"); sc2.seticon("piggybank.png"); savingscat sc3 = new savingscat(); sc3.setname("others"); sc3.seticon("moneyjar.png"); savingscatdatasource scdatasource = new savingscatdatasource(db); scdatasource.insertsavingscat(sc1); scdatasource.insertsavingscat(sc2); scdatasource.insertsavingscat(sc3); } }
savingscat
package com.mutesoft.savings; public class savingscat { private int id; private string name; private string icon; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string geticon() { return icon; } public void seticon(string icon) { this.icon = icon; } }
savingscatdatasource
package com.mutesoft.savings; import java.util.arraylist; import android.content.contentvalues; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import com.mutesoft.sqlitedb.mysqlitehelper; public class savingscatdatasource { sqlitedatabase db; public savingscatdatasource(sqlitedatabase db) { this.db = db; } public savingscat getsavingscat(long id) { cursor cursor = db.rawquery("select * " + mysqlitehelper.table_savingscat + " " + mysqlitehelper.col_savingscat_id + " = " + id, null); cursor.movetofirst(); return cursortosavingscat(cursor); } public arraylist<savingscat> getallsavingscat() { arraylist<savingscat> alp = new arraylist<savingscat>(); cursor cursor; cursor = db.rawquery( "select * " + mysqlitehelper.table_savingscat, null); cursor.movetofirst(); while (!cursor.isafterlast()) { alp.add(cursortosavingscat(cursor)); cursor.movetonext(); } cursor.close(); return alp; } public void insertsavingscat(savingscat sc) { contentvalues values = new contentvalues(); values.put(mysqlitehelper.col_savingscat_name, sc.getname()); values.put(mysqlitehelper.col_savingscat_icon, sc.geticon()); db.insert(mysqlitehelper.table_savingscat, null, values); } public void updatesavingscat(savingscat sc) { string query = "update " + mysqlitehelper.table_savingscat + " set " + mysqlitehelper.col_savingscat_name + " = '" + sc.getname() + "', " + mysqlitehelper.col_savingscat_icon + " = '" + sc.geticon() + "', " + mysqlitehelper.col_savingscat_id + " = " + sc.getid(); db.execsql(query); } public void eliminasavingscat(long id) { db.delete(mysqlitehelper.table_savingscat, mysqlitehelper.col_savingscat_id + " = " + id, null); } public savingscat cursortosavingscat(cursor cursor) { savingscat sc = new savingscat(); sc.setid(cursor.getint(0)); sc.setname(cursor.getstring(1)); sc.seticon(cursor.getstring(2)); return sc; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" android:orientation="vertical" android:scaletype="center" > <gridview android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/lay" android:layout_gravity="center" android:columnwidth="100dp" android:horizontalspacing="5dip" android:listselector="@layout/selectors" android:numcolumns="3" android:padding="5dp" android:scaletype="fitcenter" android:stretchmode="columnwidth" android:verticalspacing="10dp" /> </linearlayout>
row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:scaletype="center" android:layout_gravity="center" > <imageview android:id="@+id/album_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:adjustviewbounds="true" android:padding="5dp" android:src="@drawable/bank1" /> <textview android:id="@+id/image_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:textsize="13sp" android:textcolor="#d0d0d0" android:text="ola" /> </linearlayout>
Comments
Post a Comment