我有活动,我可以在其中放入2个文本并保存在DB中 我有3个标签,每个标签都有不同的图片 请有人帮我提供代码,我可以在每个标签上显示这2个文本!我不知道怎么能成功!
SQLITE ACTIVITY:
TAB1:
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "big.db";
public static final String TABLE_NAME = "images";
public static final String NAME = "name";
public static final String PLACE = "place";
public static final String KEY_ID = "id";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_IMAGES_TABLE = "CREATE TABLE images ( " +
"id" + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"place TEXT )";
db.execSQL(CREATE_IMAGES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS images");
this.onCreate(db);
}
public void insertentry(String name, String place) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NAME, name);
contentValues.put(PLACE, place);
db.insert(TABLE_NAME, null, contentValues);
}
}
public class Tab1 extends Fragment {
DataBaseHelper db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab1, container, false);
ImageView imageView = (ImageView) v.findViewById(R.id.img1);
String photoPath = Environment.getExternalStorageDirectory() + "/Download/image1.jpg";
Bitmap bitmap1 = BitmapFactory.decodeFile(photoPath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
imageView.setImageBitmap(b);
return v;
}
答案 0 :(得分:1)
只需使用以下代码来检索表中的数据
class DbResponse{
public String name;
public String place;
}
现在
public DbResponse getData(){
DbResponse obj = new DbResponse();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
obj.name = cursor.getString(cursor.getColumnIndex(NAME));
obj.place = cursor.getString(cursor.getColumnIndex(PLACE));
}
cursor.close();
return obj;
}
你的片段类中的写
DataBaseHelper db = new DataBaseHelper(getActivity());
DbResponse response = db.getData();
textview.setText(response.name+" - "+response.place);
希望它会帮助你