到目前为止,我有一个Android应用程序,可以将数据(简单文本)保存到sqlite数据库,并将其显示在列表视图中。我希望能够添加图像,但我不知道如何。
这里是每次添加一些文本时创建行的类:
public class TestDatabaseActivity扩展ListActivity { 私有的CommentsDataSource数据源;
int holanumero;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_database);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
final ListView listadeutiles = (ListView) findViewById(android.R.id.list);
//
listadeutiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
holanumero = position;
}});
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId())
{
case R.id.add:
EditText texto1 = (EditText) findViewById(R.id.editText1);
String palabra1 = texto1.getText().toString();
EditText texto2 = (EditText) findViewById(R.id.editText2);
String palabra2 = texto2.getText().toString();
String palabra3 = palabra1 + palabra2;
comment = datasource.createComment(palabra3);
adapter.add(comment);
texto1.setText("");
texto2.setText("");
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
comment = (Comment) getListAdapter().getItem(holanumero);
datasource.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
答案 0 :(得分:0)
在数据库中保存图像很少是个好主意。您应该将文件路径存储到SD卡中的图像,然后将该图像加载到getView()
适配器中的ListView
方法中。