如何在片段中显示sqlite数据..!
How to display sqlite data in fragments..!
我有 activity 我可以在其中放入 2 个文本并保存在数据库中
我有 3 个选项卡,每个选项卡中都有不同的图片
请有人可以帮助我的代码,我可以在每个选项卡上显示这 2 个文本!不知道怎么办!
SQLITE ACTIVITY:
选项卡 1:
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;
}
只需使用以下代码从您的 table
中检索数据
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;
}
在你的片段中class写
DataBaseHelper db = new DataBaseHelper(getActivity());
DbResponse response = db.getData();
textview.setText(response.name+" - "+response.place);
希望对您有所帮助
我有 activity 我可以在其中放入 2 个文本并保存在数据库中 我有 3 个选项卡,每个选项卡中都有不同的图片 请有人可以帮助我的代码,我可以在每个选项卡上显示这 2 个文本!不知道怎么办!
SQLITE ACTIVITY:
选项卡 1:
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;
}
只需使用以下代码从您的 table
中检索数据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;
}
在你的片段中class写
DataBaseHelper db = new DataBaseHelper(getActivity());
DbResponse response = db.getData();
textview.setText(response.name+" - "+response.place);
希望对您有所帮助