【发布时间】:2021-03-27 23:00:45
【问题描述】:
所以只是为了描述正在构建的应用程序,主屏幕上有 3 个按钮,“数据输入”、“信息显示”和“退出”。当用户单击“数据输入”时,他们可以在一些 EditText 框中填写信息,并从图库中上传图像。一旦他们单击页面上的“保存”按钮,所有输入的数据都将发送到“信息显示”屏幕中的 ListView 小部件。但是我现在的问题是我已经设法通过使用 Sqlite 将 EditText 值获取到列表视图,但我目前不知道如何获取从画廊上传到 ListView 小部件的图像
这些是我拥有的工作代码(仅缺少图像部分)
旅游助手
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TouristHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "assignment.db";
private static final int SCHEMA_VERSION = 1;
public TouristHelper(Context context){
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
//will be called once when the database 1s not created
db.execSQL("CREATE TABLE tourist_table ( _id INTEGER PRIMARY KEY AUTOINCREMENT," +
" Vactivity TEXT, Vdate TEXT, Vdescription TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//Will not be called until SCHEMA_VERSION increases
// Here we can upgrade the database e.g. add more tables
}
/*Read all records from restaurants_Table */
public Cursor getAll() {
return (getReadableDatabase().rawQuery(
"SELECT _id, Vactivity, Vdate, Vdescription " +
"FROM tourist_table ORDER BY Vactivity", null));
}
public Cursor getById(String id) {
String[] args = {id};
return (getReadableDatabase().rawQuery(
"SELECT _id, Vactivity, Vdate, Vdescription" +
" FROM tourist_table WHERE _ID = ?", args));
}
public void insert (String Vactivity, String Vdate,
String Vdescription) {
ContentValues cv = new ContentValues();
cv.put("Vactivity", Vactivity);
cv.put("Vdate", Vdate);
cv.put("Vdescription", Vdescription);
getWritableDatabase().insert("tourist_table", "Vactivity", cv);
}
public void update(String id, String Vactivity, String Vdate,
String Vdescription) {
ContentValues cv = new ContentValues();
String[] args = {id};
cv.put("Vactivity", Vactivity);
cv.put("Vdate", Vdate);
cv.put("Vdescription", Vdescription);
getWritableDatabase().update("tourist_table", cv, "_ID = ?", args);
}
public String getID(Cursor c) { return (c.getString(0)); }
public String getActivity(Cursor c){
return (c.getString(1));
}
public String getDate(Cursor c){
return (c.getString(2));
}
public String getDescription(Cursor c){
return (c.getString(3));
}
}
信息显示
public class InfoDisplay extends AppCompatActivity {
private Cursor model = null;
private VietnamAdapter adapter = null;
private ListView list;
private TouristHelper helper = null;
private TextView empty = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_display);
empty = findViewById(R.id.empty);
helper = new TouristHelper(this);
list = findViewById(R.id.listview);
model = helper.getAll();
adapter = new VietnamAdapter(this, model, 0);
list.setOnItemClickListener(onListClick);
list.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
if (model != null) {
model.close();
}
model = helper.getAll();
if (model.getCount() > 0) {
empty.setVisibility(View.INVISIBLE);
}
adapter.swapCursor(model);
}
@Override
protected void onDestroy() {
helper.close();
super.onDestroy();
}
private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
model.moveToPosition(position);
String recordID = helper.getID(model);
Intent intent;
intent = new Intent(InfoDisplay.this, DataIn.class);
intent.putExtra("ID", recordID);
startActivity(intent);
}
};
static class DataHolder {
private TextView VietACTI = null;
private TextView VietDATE = null;
private TextView VietDES = null;
DataHolder(View row) {
VietACTI = row.findViewById(R.id.VAct);
VietDATE = row.findViewById(R.id.VDate);
VietDES = row.findViewById(R.id.VDes);
}
void populateFrom(Cursor c, TouristHelper helper) {
VietACTI.setText(helper.getActivity(c));
VietDATE.setText(helper.getDate(c));
VietDES.setText(helper.getDescription(c));
}
}
class VietnamAdapter extends CursorAdapter {
VietnamAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
DataHolder holder = (DataHolder) view.getTag();
holder.populateFrom(cursor, helper);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
DataHolder holder = new DataHolder(row);
row.setTag(holder);
return (row);
}
}
}
数据输入
public class DataIn extends AppCompatActivity {
private EditText Vactivity;
private EditText Vdate;
private EditText Vdescription;
private Button buttonSave;
private Button buttonImg;
ImageView imageView;
private Bitmap imageToStore;
private static int RESULT_LOAD_IMAGE = 1;
private TouristHelper helper = null;
private String touristID = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_input);
Vactivity = findViewById(R.id.activity);
Vdate = findViewById(R.id.date);
Vdescription = findViewById(R.id.description);
buttonSave = findViewById(R.id.button_save);
buttonSave.setOnClickListener(onSave);
buttonImg = findViewById(R.id.imginput);
buttonImg.setOnClickListener(onImg);
imageView = findViewById(R.id.imgView);
helper = new TouristHelper(this);
touristID = getIntent().getStringExtra("ID");
if (touristID != null) {
load();
}
/*list = findViewById(R.id.Data);
adapter = new DataAdapter();
list.setAdapter(adapter);*/
}
@Override
protected void onDestroy() {
super.onDestroy();
helper.close();
}
private void load() {
Cursor c = helper.getById(touristID);
c.moveToFirst();
Vactivity.setText(helper.getActivity(c));
Vdate.setText(helper.getDate(c));
Vdescription.setText(helper.getDescription(c));
}
View.OnClickListener onImg = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
/*Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE*/);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "pick an image"), RESULT_LOAD_IMAGE);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
/*String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();*/
// String picturePath contains the path of selected Image
imageView.setImageURI(selectedImage);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
}
}
private View.OnClickListener onSave = new View.OnClickListener(){
@Override
public void onClick(View v){
//read data from restaurantName EditText
String nameStr = Vactivity.getText().toString();
String dateStr = Vdate.getText().toString();
String desStr = Vdescription.getText().toString();
if (touristID == null) {
helper.insert(nameStr, dateStr, desStr);
}
else {
helper.update(touristID, nameStr, dateStr, desStr);
}
// To clase current Activity class and exit
finish();
}
};
}
【问题讨论】:
标签: android sql sqlite android-studio android-sqlite