0
回答
简单使用 SimpleCursorAdapter
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

原文转自:http://marshal.easymorse.com/archives/4105

如果使用Sqlite,建议和ContentProvider结合使用。这样数据库的生命周期就不用自己管了。然后,如果要在比如ListView中显示,可以使用CursorAdapter。简化的办法是使用子类SimpleCursorAdapter。

以下就介绍一下使用sqlite+content provider+cursor adapter的最简单实现示例。太简单了,示例如图:

image

首先,要有个Content provider,如不了解如何实现,请参考编写最简单的Content Provider在Content provider实现中使用SQLiteOpenHelper,下面写的是结合二者的:

public class RiverContentProvider extends ContentProvider {

    public static final Uri CONTENT_URI = Uri 
            .parse("content://com.easymorse.cp.rivers");

    public static final String _ID = "_id";

    public static final String NAME = "name";

    public static final String LENGTH = "length";

    private static SQLiteDatabase database;

    private static final int DATABASE_VERSION = 2;

    private static final List<River> RIVERS = new ArrayList<River>();

    static { 
        River river = new River("长江", 6380); 
        RIVERS.add(river);

        river = new River("黄河", 5464); 
        RIVERS.add(river); 
    }

    @Override 
    public int delete(Uri uri, String selection, String[] selectionArgs) { 
        // TODO Auto-generated method stub 
        return 0; 
    }

    @Override 
    public String getType(Uri uri) { 
        // TODO Auto-generated method stub 
        return null; 
    }

    @Override 
    public Uri insert(Uri uri, ContentValues contentValues) { 
        // TODO Auto-generated method stub 
        return null; 
    }

    @Override 
    public boolean onCreate() { 
        database = new RiverDatabaseHelper(getContext(), "rivers", null, 
                DATABASE_VERSION).getReadableDatabase(); 
        return database != null; 
    }

    @Override 
    public Cursor query(Uri uri, String[] projection, String selection, 
            String[] selectionArgs, String sortOrder) { 
        return database.query("rivers", projection, selection, selectionArgs, 
                null, null, sortOrder); 
    }

    @Override 
    public int update(Uri uri, ContentValues values, String selection, 
            String[] selectionArgs) { 
        // TODO Auto-generated method stub 
        return 0; 
    }

    private static class RiverDatabaseHelper extends SQLiteOpenHelper {

        public RiverDatabaseHelper(Context context, String name, 
                CursorFactory factory, int version) { 
            super(context, name, factory, version); 
        }

        @Override 
        public void onCreate(SQLiteDatabase database) { 
            database.execSQL("create table if not exists rivers(" 
                    + " _id integer primary key autoincrement," + " name text," 
                    + "length integer" + ");");

            SQLiteStatement statement = database 
                    .compileStatement("insert into rivers(name,length) values(?,?)");

            for (River r : RIVERS) { 
                int index = 1; 
                statement.bindString(index++, r.getName()); 
                statement.bindLong(index++, r.getLength()); 
                statement.executeInsert(); 
            }

            statement.close(); 
        }

        @Override 
        public void onUpgrade(SQLiteDatabase database, int oldVersion, 
                int newVersion) { 
            database.execSQL("drop table if exists rivers"); 
            onCreate(database); 
        }

    }

这里写的很简略,没用到的方法都没实现。

在总的布局中使用了ListView:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView android:id="@+id/riverList" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
</LinearLayout>

使用了自定义的ListView布局,见:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="wrap_content"> 
    <TextView android:id="@+id/riverName" android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
</LinearLayout>

最后是在Activity中使用contentprovider查询的cursor,生成ListView:

public class ListViewActivity extends Activity {

    private ListView riverListView;

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        setRiverListViewAdapter(); 
    }

    private void setRiverListViewAdapter() { 
        riverListView = (ListView) this.findViewById(R.id.riverList);

        Cursor cursor = managedQuery(RiverContentProvider.CONTENT_URI, null, 
                null, null, null); 
        CursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, 
                cursor, new String[] { RiverContentProvider.NAME }, 
                new int[] { R.id.riverName }); 
        riverListView.setAdapter(adapter); 
    } 
}

源代码见:

http://easymorse.googlecode.com/svn/tags/CustomListViewDemo-0.1/

顶部