Android的ListView的使用SQLite [英] Android ListView with SQLite

查看:375
本文介绍了Android的ListView的使用SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想刷新列表视图项目。这些项目是从SQLite数据库填充。我的code是低于

I'd like to refresh the Listview items. These items are populated from SQLite database. My code is below

public class Weeve extends Activity {
      private String[] lv_arr;
    protected ListView CView;
    private DBAdapter mDbHelper;

公共无效的onCreate(捆绑savedInstanceState){

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

   mDbHelper = new DBAdapter(this);
    mDbHelper.open();

    Cursor c = mDbHelper.getAll();
    if (c.getCount() > 0)
    {if (c.moveToFirst())
    {
        ArrayList strings = new ArrayList();
       do {                    
          String mC = c.getString(0);
          strings.add(mC);  

       } while (c.moveToNext());
       lv_arr = (String[]) strings.toArray(new String[strings.size()]);
    }
    }  
    else Toast.makeText(this, 
           "No more Records", 
           Toast.LENGTH_SHORT).show();
    c.close();

    ListView CView = new ListView(this);
    CView.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, lv_arr));      
    setContentView(CView);}}

我想提神添加,更新或删除SQLite表后,这个列表视图。这些操作由内容或选项菜单中调用。我试图创建这些code到一个分离的功能,每次操作后调用它。但不能。我想的setContentView(CView的)语句。

I'd like to make refreshing this list view after adding, updating or deleting SQLite table. These operations are called by content or option menu. I tried to create these code into a separated function and call it after every operation. But can't. I think setContentView(CView) statement.

我也试过用SimpleCursorAdapter像Android.com记事本样本。我得到了线程错误。帮帮我吧。

I also tried to use SimpleCursorAdapter like notepad sample from Android.com. I got Thread error. Help me.

推荐答案

您可以做到这一点使用Adapter.notifyDataSetChanged()。

You can do that using Adapter.notifyDataSetChanged().

不过,我觉得你的code稍微偏离。你从数据库中使用游标,就创建行值一个新的ArrayAdapter获取行。为什么不使用的CursorAdapter来支持你的名单?这就是它的意思了。

However, I find your code slightly off. You're fetching rows from the database using a cursor, just to create a new ArrayAdapter for the row values. Why not use a CursorAdapter to back your list? That's what it's meant for.

这篇关于Android的ListView的使用SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆