如何将图像添加到 sqlite 列表视图



到目前为止,我有一个Android应用程序,可以将数据(简单文本)保存到SQLite数据库中并将其显示在列表视图中。我也希望能够添加图像,但我不知道如何添加。

这是每次添加一些文本时创建行的类:

公共类 TestDatabaseActivity 扩展 ListActivity { 私有评论数据源数据源;

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();
  }

将图像保存在数据库中很少是我的好主意。您应该将图像的文件路径存储在 SD 卡中,然后在ListView适配器中的getView()方法中加载该图像。

最新更新