在 ListActivity 中显示 SQLite 数据库中的数据

发布于 2024-10-31 13:46:57 字数 4556 浏览 7 评论 0原文

我的 Android 应用程序由多个 Activity、一个服务(与远程服务器通信)和一个 SQLite 数据库组成。我在向用户显示数据库表中的数据时遇到很多问题。

我做了很多谷歌搜索,并且尝试了 listView 和 tableView。 第一个,经过多次尝试,效果不错。 OffViaggListActivity 类绑定到应用程序中的唯一服务(称为 Servizio),然后,当绑定完成时,它会调用 Servizio 上的方法 (eseguiComandoLocale(...)),该方法对 SQLite 数据库进行查询并返回listView 中使用的光标。

第一个问题:它现在只显示表的最后一个字段。 我想显示所有九个字段,并允许用户在选择一个字段时启用某些功能(例如“删除行”)。

第二个问题:如果我关闭活动然后重新打开它,列表仍为空,如果我再次关闭活动,我会收到“IllegalArgumentException:服务未注册”。

代码:

1)ListActivity:

public class OffViaggListActivity extends ListActivity {

    static Servizio mioServizio;
    ServiceConnection myConn;

    ListView lista;
    Button chiudi;

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.offerteviaggilist);

            lista=(ListView) findViewById(android.R.id.list);
            chiudi=(Button) findViewById(R.id.OffViaggChiudiButton);

            chiudi.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    OffViaggListActivity.this.unbundService();
                    OffViaggListActivity.this.finish();
                }
            });

            if(mioServizio==null){
            myConn=new ServiceConnection() {

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    // TODO Auto-generated method stub
                    mioServizio=null;
                    Log.d("OffViaggListActivit", "Servizio disconnesso");
                }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                    // TODO Auto-generated method stub
                    Servizio.LocalBinder binder=(Servizio.LocalBinder)service;
                mioServizio=binder.getService();
                Log.d("OffViaggListActivit", "Servizio connesso");

                    String[] displayFields = new String[] {DBSQLite.OFFV_KEY_ROWID,
                            DBSQLite.OFFV_KEY_USER,DBSQLite.OFFV_KEY_PART,
                            DBSQLite.OFFV_KEY_DEST,DBSQLite.OFFV_KEY_DATA,
                            DBSQLite.OFFV_KEY_ORA,DBSQLite.OFFV_KEY_POSTI,
                                DBSQLite.OFFV_KEY_COSTO,DBSQLite.OFFV_KEY_DETTAGLI};


                 int[] displayViews = new int[] { android.R.id.text1, 
                                                 android.R.id.text1,                                             
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1
                                                 };

                  Cursor cur=mioServizio.eseguiComandoLocale
                                         (OffViaggListActivity.this,"OffertePubblicate");

                    setListAdapter(new SimpleCursorAdapter
                                         (OffViaggListActivity.this.getApplicationContext(), 
                             android.R.layout.simple_list_item_1, cur, 
                             displayFields, displayViews));
                }
            };

            bindService(new Intent(OffViaggListActivity.this, Servizio.class), 
                                    myConn, Context.BIND_AUTO_CREATE);
            }

       }



        public void unbundService(){
            unbindService(myConn);
        }

}

2)XML 布局:

<?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="match_parent" android:orientation="vertical">
        <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@id/android:list"></ListView>
        <Button android:layout_height="wrap_content" android:id="@+id/OffViaggChiudiButton" android:text="@string/chiudi" android:layout_width="match_parent"></Button>
    </LinearLayout>

如果我犯了语法或格式错误,请原谅我。

my android application is formed by several activity, one service (that communicate with a remote server) and a SQLite database. I have a lot of problem to display data from a table of the database to the user.

I made a lot of googling and i tried both listView and tableView.
The first one, after a lot of attemps, works a few.
The class OffViaggListActivity make a bind to the unique service in the application (called Servizio) then, when the bind is done, it call a method on Servizio (eseguiComandoLocale(...)) that make a query on the SQLite database and return a Cursor used in the listView.

FIRST PROBLEM: It now display only the last field of the table.
I want to display all the nine field and make possible to the user to enable some funcions (such as "delete row") when the user select one.

SECOND PROBLEM: If i close the activity and then reopen it, the list remains empty, and if i close the activity again i get "IllegalArgumentException: Service Not Registered".

Code:

1) ListActivity:

public class OffViaggListActivity extends ListActivity {

    static Servizio mioServizio;
    ServiceConnection myConn;

    ListView lista;
    Button chiudi;

     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.offerteviaggilist);

            lista=(ListView) findViewById(android.R.id.list);
            chiudi=(Button) findViewById(R.id.OffViaggChiudiButton);

            chiudi.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    OffViaggListActivity.this.unbundService();
                    OffViaggListActivity.this.finish();
                }
            });

            if(mioServizio==null){
            myConn=new ServiceConnection() {

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    // TODO Auto-generated method stub
                    mioServizio=null;
                    Log.d("OffViaggListActivit", "Servizio disconnesso");
                }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                    // TODO Auto-generated method stub
                    Servizio.LocalBinder binder=(Servizio.LocalBinder)service;
                mioServizio=binder.getService();
                Log.d("OffViaggListActivit", "Servizio connesso");

                    String[] displayFields = new String[] {DBSQLite.OFFV_KEY_ROWID,
                            DBSQLite.OFFV_KEY_USER,DBSQLite.OFFV_KEY_PART,
                            DBSQLite.OFFV_KEY_DEST,DBSQLite.OFFV_KEY_DATA,
                            DBSQLite.OFFV_KEY_ORA,DBSQLite.OFFV_KEY_POSTI,
                                DBSQLite.OFFV_KEY_COSTO,DBSQLite.OFFV_KEY_DETTAGLI};


                 int[] displayViews = new int[] { android.R.id.text1, 
                                                 android.R.id.text1,                                             
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1,
                                                 android.R.id.text1
                                                 };

                  Cursor cur=mioServizio.eseguiComandoLocale
                                         (OffViaggListActivity.this,"OffertePubblicate");

                    setListAdapter(new SimpleCursorAdapter
                                         (OffViaggListActivity.this.getApplicationContext(), 
                             android.R.layout.simple_list_item_1, cur, 
                             displayFields, displayViews));
                }
            };

            bindService(new Intent(OffViaggListActivity.this, Servizio.class), 
                                    myConn, Context.BIND_AUTO_CREATE);
            }

       }



        public void unbundService(){
            unbindService(myConn);
        }

}

2) XML Layout:

<?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="match_parent" android:orientation="vertical">
        <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@id/android:list"></ListView>
        <Button android:layout_height="wrap_content" android:id="@+id/OffViaggChiudiButton" android:text="@string/chiudi" android:layout_width="match_parent"></Button>
    </LinearLayout>

Forgive me if i made syntax or formatting error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月朦胧 2024-11-07 13:46:57

1) SimpleCursorAdapter 的布局 id 参数(即第二个参数)应该表示游标查询返回的每条记录的布局。

在这里,您返回 simple_list_item_1,它只是一个 TextView。您将无法使用此布局显示多个字段。

您需要创建自己的布局,其中包含 9 个 TextView,当然还有 9 个不同的 ID,您可以在 displayViews 数组中设置它们,并将该布局的 id 提供给 SimpleCursorAdapter。

2) 抱歉,我还没有使用过服务。查看 Android 文档,应该在 onDestroy 方法中调用 unbindService。在这里,您有一个“unbundService”方法。它是从 onDestroy 调用的吗?

1) The layout id parameter, i.e. the 2nd parameter, to the SimpleCursorAdapter is supposed to represent a layout for each record returned by the cursor query.

Here, you are returning simple_list_item_1 which is simply a single TextView. You won't be able to display more than one field with this layout.

You need to create your own layout, with 9 TextViews, with of course 9 different IDs, that you set in your displayViews array and give the id of this layout to SimpleCursorAdapter.

2) Sorry, I have not worked with services yet. Looking at Android docs, one should call unbindService in the onDestroy method. Here, you have a "unbundService" method. Is it called from onDestroy ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文