使用sqlite资产助手从数据库填充listview,而不扩展listactivity

时间:2014-07-01 16:34:13

标签: java android sqlite listview android-listview

使用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上的listview中显示..

经过大量的研究后,我实际上没有使用自定义适配器的代码来填充列表视图。 我想在listview的每一行中显示两列数据。 每行的标题将是来自名为“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();
          }
    });
}}

登录CAT

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>

2 个答案:

答案 0 :(得分:1)

使用CursorAdapter怎么样?还有SimpleCursorAdapter

  

标准构造函数。

     

参数    context ListView与此关联的上下文   SimpleListItemFactory正在运行布局

     

的资源标识符   布局文件,用于定义此列表项的视图。布局文件   应至少包括“to”

中定义的那些命名视图      

c   数据库游标。如果光标尚不可用,则可以为null。

     

来自表示要绑定到UI的数据的列名列表。   如果光标尚不可用,则可以为null。

     

这些观点   应显示“from”参数中的列。这些应该都是   TextViews。此列表中的前N个视图被赋予了该值的值   from参数中的前N列。如果光标是,则可以为null   还不可用。 flags用于确定行为的标志   适配器,根据CursorAdapter(Context,Cursor,int)。

ListView listView = (ListView) findViewById(...);
listView.setAdapter(new SimpleCursorAdapter(
   this,  // The context
   R.layout.layoutToShow,  // The layout of the row to show
   dbOpen.getUnits(),  // Here the cursor where the data is, i used .getUnits()
                       // but it can be anything
   new String[] { tables to show },  // here the tables to show
   new int[] { where to put data? },  // here where to show? the IDs
                                     // of the layout elements where put data.
   0
);

查看SimpleCursorAdapter以获取更多信息,或更多通用课程,如果您需要CursorAdapter

,可为您提供更多选项

答案 1 :(得分:-1)

你应该这样做:

public Class Units{
    String utopicname;
    public Units(Strign utopicname){
         this.utopicname = utopicname;
    }
    public void setUtopicName(String utopicname){
         this.utopicname=utopicname;
    }
    public String getUtopicName(){
         return utopicname;
    }
}

//////////////////////////

ArrayList<Units> units;
Cursor c = dbOpen.getUnits();
do{
    Units unit = new Units(c.getString(2));
    units.add(unit);
}while(c.moveToNext());

ListView listview = (ListView) findViewById(R.id.listView1);
AdapterLists adapterlists = new AdapterLists(this,R.layout.main_layout, units);
listview.setAdapter(adapterlists);

class AdapterLists extends ArrayAdapter {
    Activity context = null;
    public AdapterLists(Activity context, int layout,ArrayList<Units> units) {
        super(context, layout, canales);
        this.context = context;
    }

    public View getView(final int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View item = inflater.inflate(R.layout.unit_row, null);
        TextView ui_unit_utopicname = (TextView)item.findViewById(R.id.txt_row);
        String dat_unit_utopicname = units.get(position).getUtopicName();
        ui_unit_utopicname.setText(dat_unit_utopicname);
        return item;
    }
}

您需要使用TextView(txt_row.xml)创建布局(在layout.xml中)