开发者

Displaying data from SQLite database in a ListActivity

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
                    mi开发者_如何学GooServizio=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.


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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜