【发布时间】:2014-07-01 16:34:12
【问题描述】:
通过使用 SQLIte Asset Helper 从数据库中获取数据... 我的数据库包含两个表- (1)android_metadata (2)topiccontent(_id,uname,utopicname)
我创建了一个单独的数据库助手类,其代码如下
public class ExternalDbOpenHelper extends SQLiteAssetHelper {
public static String DB_NAME = "cosmcontent.db";
public ExternalDbOpenHelper(Context context) {
super(context, DB_NAME, null, 1);}
public Cursor getUnits()
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String [] sqlSelect = {"_id","uname","utopicname"};
String sqlTables = "unitcontent";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, "utopicname ASC");
c.moveToFirst();
return c;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}
所以我想在我的 MainAcitvity 的列表视图中显示来自这里的数据..
经过大量研究后,我实际上并没有解决将填充列表视图的自定义适配器的代码。 我想在列表视图的每一行中显示两列数据。 每行的标题将是来自名为“utopicname”列的数据,其后的数据将来自列“uname”。 我正在尝试按升序显示“utpoicname”列中的内容。 因此确实希望内容 frin 列“uname”应该与“utopicname”排序顺序相关。 我不希望在我的 MainActivity 上扩展 listactivity。 任何帮助将不胜感激。 我的 MainActivity 的代码如下-
public class MainActivity extends Activity
{
private Cursor units;
private ExternalDbOpenHelper dbOpen;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbOpen = new ExternalDbOpenHelper(this);
units = dbOpen.getUnits();
ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleCursorAdapter(this,R.layout.eaach_row,units,
new String[]{ExternalDbOpenHelper.KEY_UTOPICNAME,ExternalDbOpenHelper.KEY_UNAME},
new int[]{R.id.topicname,R.id.unitname},0));
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v,int position, long id)
{
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
}
});
}}
记录猫
07-01 18:34:12.900: W/SQLiteAssetHelper(1017): copying database from assets...
07-01 18:34:12.900: W/SQLiteAssetHelper(1017): database copy complete
07-01 18:34:12.940: I/SQLiteAssetHelper(1017): successfully ope cosmcontent.db
07-01 18:34:13.032: D/AndroidRuntime(1017): Shutting down VM
07-01 18:34:13.032: W/dalvikvm(1017): threadid=1: thread exiting with uncaught exception (group=0xa6224288)
07-01 18:34:13.036: E/AndroidRuntime(1017): FATAL EXCEPTION: main
07-01 18:34:13.036: E/AndroidRuntime(1017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutscosm/com.example.tutscosm.MainActivity}: java.lang.NullPointerException
07-01 18:34:13.036: E/AndroidRuntime(1017):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.os.Looper.loop(Looper.java:137)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-01 18:34:13.036: E/AndroidRuntime(1017): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 18:34:13.036: E/AndroidRuntime(1017): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 18:34:13.036: E/AndroidRuntime(1017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-01 18:34:13.036: E/AndroidRuntime(1017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-01 18:34:13.036: E/AndroidRuntime(1017): at dalvik.system.NativeStart.main(Native Method)
07-01 18:34:13.036: E/AndroidRuntime(1017): Caused by: java.lang.NullPointerException
07-01 18:34:13.036: E/AndroidRuntime(1017): at com.example.tutscosm.MainActivity.onCreate(MainActivity.java:33)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.Activity.performCreate(Activity.java:5008)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-01 18:34:13.036: E/AndroidRuntime(1017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-01 18:34:13.036: E/AndroidRuntime(1017): ... 11 more
activity_main.xml
<?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/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
eaach_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/topicname"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/unitname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/topicname"
android:layout_centerVertical="true"
/>
</RelativeLayout>
【问题讨论】:
-
究竟什么是空?
-
@njzk2 不用担心这个查询了朋友..我用答案弄明白了...谢谢你的关心
标签: java android sqlite listview android-listview