2012-02-22 5 views
2

コマンドを使用してListViewにAndroidを移植する際に問題が発生しました。AndroidでrawQueryコマンドを使用してリストビューを作成できません

私は正しく動作していると思われるassetsフォルダから自分のデータベースをコピーする方法を使用しています。私はこれがDDMSで起こるのを見ているので、データベースは正しくコピーされています。私はまた、データベースが正しく開かれていると私に言っているlog.iのエントリを取得しています。 Logcatを使用してもエラーは発生しませんが、何も私のListViewに入力されていません。

DBに問い合わせて報告するには、以下のコマンドに何か問題がありますか?

ご協力いただければ幸いです。ここで

 private void fillData() { 
     Cursor c = myDbHelper.rawQuery("SELECT name FROM sqlite_master", null); 
      startManagingCursor(c); 
     // Create an array to specify the fields we want to display in the list. 
      String[] from = new String[]{"name"}; 

      // an array of the views that we want to bind those fields too. 
      int[] to = new int[]{R.id.listview}; 

      // Now create a simple cursor adapter and set it to display 
      SimpleCursorAdapter notes = 
      new SimpleCursorAdapter(this, R.layout.list_view, c, from, to); 
      setListAdapter(notes);    
     } 

は、私は、リストビューの結果を移入したい私のlists.xmlファイルです:

 <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 
     <TextView 
      android:text = "Lists" 
      style="@style/HeaderText" 
      android:textSize="15dp" 
      android:gravity="center" /> 
      <View 
      android:layout_width="wrap_content" 
      android:background="@drawable/gradient" 
      android:layout_height="1dp" /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

     <EditText 
       android:id="@+id/searchText" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 

     <Button 
       android:id="@+id/searchButton" 
       android:text="Search" 
       android:background = "@drawable/main_buttons" 
       style="@style/ButtonText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" />" 

    </LinearLayout> 
     <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 

      <TextView android:id="@android:id/empty" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#000000" 
       android:text="No data" 
       style="@style/ButtonText"/> 
     </LinearLayout> 

     </LinearLayout> 

そして最後に、list_viewレイアウトがrawQueryから正しいリストを移入するだけTextViewですコマンド。

+0

'listview'と呼ばれる' list_view'の 'TextView'はあなたですか? –

+0

はい、そうです。私はちょうどここにそれをコピーしていない。 – ADK

+1

@ADKこのメソッドを 'fillData()'と呼んでもよろしいですか? 'SimpleCursorAdapter'は' _id'( 'INTEGER PRIMARY KEY AUTOINCREMENT')フィールドの一部として('あなたがあなたのクエリに持っていない) 'これを基にしたカーソルが' Exception'を投げるべきであることを要求します。また、 'rawQuery'で何をしたいのですか?' SQLite'データベースのテーブルの名前を取得しますか? – Luksprog

答えて

1

あなたはこのfillData()方法を試みることができる:

private void fillData() { 
     Cursor c = myDbHelper.rawQuery("SELECT name FROM sqlite_master", null); 
     ArrayList<String> values = new ArrayList<String>(); 
     while (c.moveToNext()) { 
      values.add(c.getString(c.getColumnIndex("name"))); 
     } 
     c.close(); 
     // Now create a simple cursor adapter and set it to display 
     ArrayAdapter<String> notes = new ArrayAdapter<String>(this, 
       R.layout.list_view, values); 
     setListAdapter(notes); 
    } 

ListView

は、カーソルがどの _id列を持つように基づいていることが必要です。しかし、そのテーブルでは _idをクエリに追加する必要はありません。したがって、生のクエリから ArrayListにカーソルをパースして、簡単な ArrayAdapterにバインドしてみることができます。

0

ここには奇妙なことがあります。 The reference

ListActivityが画面の中央にある単一、 フルスクリーンリストで構成され、デフォルトのレイアウトを持っていると言います。ただし、希望する場合は、 onCreate()のsetContentView()で独自の表示レイアウト を設定して、画面レイアウトをカスタマイズできます。これを行うには、独自のビューMUST はidを持つListViewのオブジェクトが含まれているが、「@android:ID /リスト」(またはそれがコードにあります 場合は、リスト)だから、

を、それは我々が同様に、@android:id/listを使用しなければならないと言いますあなたがやる。

ノートブックのチュートリアルに従っているとします。代わりに@+id/android:list:CoSは、私は、だから私は自分のネーミングを使用私は

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"> 
... some other elements here 
    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textFilterEnabled="true" > 
    </ListView> 
... some other elements here 

</LinearLayout> 

ように私のListActivityなめらかために私自身のレイアウト(​​)を持っている同様のケースがあります。次に私はListActivityのコンテンツビューを明示的にsetContentView(R.layout.entries_list);に設定しています。これは私のために働く。多分それを試すことができます。

希望します。あなたは、リスト内のテーブル名を表示したい場合は

関連する問題