【问题标题】:populating listview from database with sqlite asset helper without extending listactivity使用 sqlite 资产帮助程序从数据库中填充列表视图,而不扩展列表活动
【发布时间】: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


【解决方案1】:

使用CursorAdapter 怎么样?还有SimpleCursorAdapter?

标准构造函数。

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

资源标识符 定义此列表项的视图的布局文件。布局文件 至少应该包括那些在“to”中定义的命名视图

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

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

应该在“from”参数中显示列。这些都应该 文本视图。此列表中的前 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,可以为您提供更多选择

【讨论】:

  • @MarcoAcieno 我尝试了你的代码,但我的日志猫显示运行时异常......它就像 java.lang.Runtime 异常。无法开始活动
  • 如果没有更多信息,我该如何帮助您?请添加logcat。
  • 检查 MainActivity.java 第 33 行。有些东西是空的。
  • 从第 33 行代码开始是----- lv = (ListView) findViewById(android.R.id.list); lv.setAdapter(new SimpleCursorAdapter(this,R.layout.eaach_row,dbOpen.getUnits(),new String[]{ExternalDbOpenHelper.KEY_UTOPICNAME,ExternalDbOpenHelper.KEY_UNAME }, new int[]{R.id.topicname,R.id.单位名称},0));
  • 你不需要“猜测”,启动调试器并检查行中的空值。
【解决方案2】:

你应该这样做:

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) 创建布局 (main_layout.xml)

【讨论】:

  • 您需要使用 setUtopicName() 和 getUtopicName() 添加 Units 类,然后,您必须添加:do {Units unit = new Units(c.getString(1));units.add(单位);} 而 (c.moveToNext());现在你有一个 Arraylist
  • 我不知道该怎么做。我刚刚开始在android中工作。如果您能进一步帮助我,将不胜感激。
  • 类 Units 变量会自动与数据库中的数据绑定?
  • 不,您在 do{ }while() 中执行此操作,为光标中的每个元素创建一个“单位”对象,单位数组是一个包含“单位”对象的对象。你懂的?如果需要,您可以通过邮件与我联系,查看我的个人资料
  • 感谢您的回答..我已经弄明白了..当然可能需要更多的刷刷..一定会联系你...
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 2023-03-13
  • 2019-12-12
  • 1970-01-01
  • 2023-03-12
  • 2016-06-09
  • 1970-01-01
相关资源
最近更新 更多