我无法将图标应用到GridView上的相应位置。我已经设法从数据库应用不同的名称,但我仍然没有意识到如何对图像做同样的事情。
当我运行应用程序时,会创建3个图标,但3个类别的图标相同(第一个bank1.png)。我尝试了一些像 icon.setImageResource(alsc.get(position).getIcon()); 这样的东西但是没有用,因为它总是说可以归结为String。我会提供一些帮助。感谢
SavingsCatA(活动)
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 the menu; this adds items to the action bar if it is 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 the number of images---
public int getCount() {
return alsc.size();
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an 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 * 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;
}
}
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>