这是adpater类
在此适配器中,我有一个原始文件夹中的静态图像,当我将收藏的项目项保存在数据库中并且颜色更改但我重新打开我的gridview颜色时,我也有一个收藏项的按钮。
public class FingersAdapter extends BaseAdapter {
private Context mContext;
int[] mimages;
private favDB favDB;
public int[] mThumbIds =new int[] {
R.raw.a1, R.raw.a2,R.raw.a3,R.raw.a5,R.raw.a6,R.raw.a7,R.raw.a8,R.raw.a9,R.raw.a10,
R.raw.a12,R.raw.a13,R.raw.a14,R.raw.a15,R.raw.a17,R.raw.a18,R.raw.a19,R.raw.a21,R.raw.a20,R.raw.a22,R.raw.a25,
R.raw.a27,R.raw.a26,R.raw.a27,R.raw.a28,R.raw.a29,R.raw.a30,R.raw.a31,R.raw.a32,R.raw.a33,R.raw.a34,R.raw.a35,R.raw.a36
,R.raw.a37,R.raw.a38,R.raw.a39,R.raw.a40,R.raw.a41,R.raw.a42,R.raw.a43,R.raw.a44,R.raw.a45
};
// Constructor
public FingersAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
// mimages= new int[]{R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a5, R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10};
if (convertView == null) {
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.home_image_view, parent, false);
// well set up the ViewHolder
viewHolder = new ViewHolderItem();
viewHolder.imageViewItem = (ImageView) convertView.findViewById(R.id.home_image_view);
viewHolder.fav=(CheckBox)convertView.findViewById(R.id.chWishList);
// store the holder with the view.
convertView.setTag(viewHolder);
} else {
// we've just avoided calling findViewById() on resource everytime
// just use the viewHolder
viewHolder = (ViewHolderItem) convertView.getTag();
}
// object item based on the position
viewHolder.imageViewItem.setImageResource(mThumbIds[position]);
viewHolder.imageViewItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putInt("position", position);
bundle.putIntArray("imageURLs", mThumbIds);
Intent intent = new Intent(mContext, ImageActivity.class);
intent.putExtras(bundle);
mContext.startActivity(intent);
}
});
viewHolder.fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
favDB = new favDB(mContext);
Integer image=mThumbIds[position];
String imagename= String.valueOf(image);
if (((CompoundButton) v).isChecked()) {
Toast.makeText(mContext, "Added to favorite", Toast.LENGTH_SHORT).show();
favDB.insertIntoTheDatabase(imagename,
imagename, "1");
} else {
Toast.makeText(mContext, "Removed to favorite", Toast.LENGTH_SHORT).show();
favDB.remove_fav(imagename);
}
}
});
return convertView;
}
static class ViewHolderItem {
ImageView imageViewItem;
CheckBox fav;
}
}
填充网格视图
公共类FingersDesign扩展了片段{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_fingers_design, container, false);
final GridView categories = root.findViewById(R.id.categories);
categories.setAdapter(new FingersAdapter(root.getContext()));
return root;
}
}
收藏的数据库类
这是我最喜欢的物品的课程,我在该课程中有三件事:一个是键,状态和图像字符串
public class favDB extends SQLiteOpenHelper {
private static int DB_VERSION = 1;
private static String DATABASE_NAME = "Favorite";
private static String TABLE_NAME = "favoriteTable";
public static String KEY_ID = "id";
public static String ITEM_IMAGE = "itemImage";
public static String FAVORITE_STATUS = "fStatus";
// dont forget write this spaces
private static String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " TEXT," + ITEM_IMAGE + " TEXT," + FAVORITE_STATUS + " TEXT)";
public favDB(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
// create empty table
public void insertEmpty() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
// enter your value
for (int x = 1; x < 11; x++) {
cv.put(KEY_ID, x);
cv.put(FAVORITE_STATUS, "0");
db.insert(TABLE_NAME, null, cv);
}
}
// insert data into database
public void insertIntoTheDatabase(String item_image, String id, String fav_status) {
SQLiteDatabase db;
db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ITEM_IMAGE, item_image);
cv.put(KEY_ID, id);
cv.put(FAVORITE_STATUS, fav_status);
db.insert(TABLE_NAME, null, cv);
Log.d("favDB Status", "Added in faviroite" + ", favstatus - " + fav_status + " - . " + cv);
}
// read all data
public Cursor read_all_data(String id) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "select * from " + TABLE_NAME + " where " + KEY_ID + "=" + id + "";
return db.rawQuery(sql, null, null);
}
// remove line from database
public void remove_fav(String id) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "UPDATE " + TABLE_NAME + " SET " + FAVORITE_STATUS + " ='0' WHERE " + KEY_ID + "=" + id + "";
db.execSQL(sql);
Log.d("remove", id.toString());
}
// select all favorite list
public Cursor select_all_favorite_list() {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " + FAVORITE_STATUS + " ='1'";
return db.rawQuery(sql, null, null);
}
}