0

我的 android 应用程序由多个活动、一个服务(与远程服务器通信)和一个 SQLite 数据库组成。将数据库表中的数据显示给用户有很多问题。

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

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

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

代码:

1)列表活动:

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>

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

4

1 回答 1

1

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

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

您需要创建自己的布局,其中包含 9 个 TextView,当然还有 9 个不同的 ID,您在 displayViews 数组中设置了这些布局,并将此布局的 ID 提供给 SimpleCursorAdapter。

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

于 2011-04-07T16:48:13.047 回答