Java Android:从数据库中插入数据(图像)并在GridView中显示
我无法将图标应用到GridView上的相应位置。我已经设法从数据库中应用了不同的名称,但我仍然没有意识到如何对图像执行相同的操作 当我运行应用程序时,会创建3个图标,但3个类别的图标是相同的(第一个是bank1.png)。我尝试了一些东西,比如icon.setImageResource(alsc.get(position.getIcon());但它不起作用,因为它总是说,这可以归因于一个字符串。我非常感谢你的帮助。谢谢 储蓄储蓄(活动) SavingsCatJava Android:从数据库中插入数据(图像)并在GridView中显示,java,android,sqlite,gridview,Java,Android,Sqlite,Gridview,我无法将图标应用到GridView上的相应位置。我已经设法从数据库中应用了不同的名称,但我仍然没有意识到如何对图像执行相同的操作 当我运行应用程序时,会创建3个图标,但3个类别的图标是相同的(第一个是bank1.png)。我尝试了一些东西,比如icon.setImageResource(alsc.get(position.getIcon());但它不起作用,因为它总是说,这可以归因于一个字符串。我非常感谢你的帮助。谢谢 储蓄储蓄(活动) SavingsCat package com.muteso
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;
}
}
保存SCATDatasource
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 * FROM "
+ MySQLiteHelper.TABLE_SAVINGSCAT + " WHERE "
+ 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 * FROM " + 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;
}
}
package com.mutesoft.savings;
导入java.util.ArrayList;
导入android.content.ContentValues;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入com.mutesoft.sqlitedb.MySQLiteHelper;
公共类SavingsCatDataSource{
sqlitedb数据库;
公共存储数据源(SQLiteDatabase db){
这个.db=db;
}
公共储蓄账户getSavingsCat(长id){
Cursor Cursor=db.rawQuery(“选择*FROM”
+MySQLiteHelper.TABLE_SAVINGSCAT+“其中”
+MySQLiteHelper.COL_SAVINGSCAT_ID+“=”+ID,null);
cursor.moveToFirst();
返回游标osavingscat(游标);
}
公共阵列列表getAllSavingsCat(){
ArrayList alp=新的ArrayList();
光标;
cursor=db.rawQuery(
“选择*自”+MySQLiteHelper.TABLE_SAVINGSCAT,null);
cursor.moveToFirst();
而(!cursor.isAfterLast()){
alp.add(光标)和osavingscat(光标));
cursor.moveToNext();
}
cursor.close();
返回alp;
}
公共空间插入SavingsCat(SavingsCat sc){
ContentValues=新的ContentValues();
value.put(MySQLiteHelper.COL_SAVINGSCAT_NAME,sc.getName());
value.put(MySQLiteHelper.COL_SAVINGSCAT_ICON,sc.getIcon());
db.insert(MySQLiteHelper.TABLE_SAVINGSCAT,null,value);
}
公共作废更新SavingsCat(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();
execSQL(查询);
}
公共无效EliminavingScat(长id){
db.delete(MySQLiteHelper.TABLE_SAVINGSCAT,
MySQLiteHelper.COL_SAVINGSCAT_ID+“=”+ID,null);
}
公共储蓄游标或储蓄游标(游标){
SavingsCat sc=新SavingsCat();
sc.setId(cursor.getInt(0));
sc.setName(cursor.getString(1));
sc.setIcon(cursor.getString(2));
返回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>
<?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>
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: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>
<?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>
在“res\drawable mdpi”上,您在哪里有xxxx.png
文件。该应用程序已从MySQLiteHelper获取其中一个图像(bank1.png)。由于图像的位置原因,我无法将其设置在正确的网格位置。然后,您应仅在db中存储xxxx
,并使用资源。getIdentifier(…,…,“xxxx”)
获取此资源的int ID。。。该DI可与setImageResource
sorry一起使用。我不太懂,你能给我看一下那段代码吗?它不需要全部完成,只需要让我知道从哪里开始或修复什么。谢谢