我创建了一个应用程序。在这个应用程序中,我想使用Getter
和Setter
方法从数据库中获取数据。我不知道我该怎么做。请帮我。必要的代码在这里:
DisplayMedia.java
public class DisplayMedia extends Activity {
ImageView displayImage;
VideoView displayVideo;
TextView txtMediaDate,txtMediaTime,txtMediaAddress;
DatabaseHelper dbHelper;
Intent intent;
String mediaPath,mediaDate,mediaTime,mediaAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_media);
displayImage = (ImageView) findViewById(R.id.displayImage);
displayVideo = (VideoView) findViewById(R.id.displayVideo);
txtMediaDate = (TextView) findViewById(R.id.txtMediaDate);
txtMediaTime = (TextView) findViewById(R.id.txtMediaTime);
txtMediaAddress = (TextView) findViewById(R.id.txtMediaAddress);
dbHelper = new DatabaseHelper(getApplicationContext());
mediaPath = dbHelper.displayImages();
File mediaFile = new File(mediaPath);
if (mediaFile.exists()) {
if (isImage(mediaPath)) {
displayVideo.setVisibility(View.GONE);
displayImage.setVisibility(View.VISIBLE);
Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
displayImage.setImageBitmap(scale);
} else {
displayImage.setVisibility(View.GONE);
displayVideo.setVisibility(View.VISIBLE);
displayVideo.setVideoURI(Uri.parse(mediaFile.toString()));
displayVideo.start();
}
}
}
public static boolean isImage(String str) {
boolean temp = false;
String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
for (int i = 0; i < arr.length; i++) {
temp = str.endsWith(arr[i]);
if (temp) {
break;
}
}
return temp;
}
}
DataBaseHelper.java
public void insertMedia(String mPath,String mDate,String mTime,String mAddress) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
cur = db.query(INCIDENT, null, null, null, null, null, null);
cur.moveToFirst();
values.put("mediaPath", mPath);
values.put("mediaDate", mDate);
values.put("mediaTime", mTime);
values.put("mediaAddress", mAddress);
db.insert(INCIDENT, null, values);
db.close();
}
public String displayMedia() {
db = this.getReadableDatabase();
cur = db.query(INCIDENT, null, null, null, null, null, null);
String path = null,date = null, time = null, address = null;
cur.moveToFirst();
while (cur.isAfterLast() == false) {
path = (cur.getString(0));
date = (cur.getString(1));
time = (cur.getString(2));
address = (cur.getString(3));
cur.moveToNext();
}
return path;
}
在这个应用程序中,我成功地在数据库中插入了数据。在我的数据库中,以下数据已存储:
MediaPath, MediaDate, MediaTime, MediaAddress
现在我想使用上面displayMedia()
获取所有数据。我怎么能这样做?
答案 0 :(得分:0)
你可以得到一个Cursor对象包含里面的所有表数据:
public Cursor getCursor() {
db = this.getReadableDatabase();
return db.query(INCIDENT, null, null, null, null, null, null);
}
然后,无论您需要什么,您都可以获得以下信息:
Cursor c = yourDatabaseClass.getCursor();
int rows = c.getCount();
for(int i=0;i<rows;i++){
String path = c.getString(0);
String date = c.getString(1);
String time = c.getString(2);
String adress = c.getString(3);
c.moveToNext();
}
然后由您决定如何显示路径/日期/时间/地址信息。