2017-01-16 7 views
0

SimpleCursorAdapterを介してデータベースからリストビューを取り込もうとしていますが、エラーが発生します。 原因:java.lang.IllegalArgumentException:列 '_id'が存在しませんandroid SimpleCursorAdapterはデータベースからリストビューを取り込みます

ここに私のリストビューを含む私のBlistActivityです。

public class BListActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_blist); 

    MyDBHandler myDBHandler = new MyDBHandler(this,null,3); 
    ListView listView = (ListView) findViewById(R.id.blistView); 
    Cursor cursor = myDBHandler.showAllRecords(); 
    String[] columnNames = new String[]{"name_number"}; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      android.R.layout.simple_list_item_1, 
      cursor, 
      columnNames, 
      new int[]{android.R.id.text1}); 
    listView.setAdapter(adapter); 
} 
} 

ここはこのアクティビティのXMLコードです。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.haroon.vuultimateblocker.BListActivity"> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/blistView" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     android:choiceMode="multipleChoice"/> 
</RelativeLayout> 

私はlistViewをmultiplechoiceとして指定する必要があることにも言及しておく必要があります。

答えて

0

何をしようとしていますか?もしそうなら、あなたはデータをリストしたいだけですか?

プライベートListView lvCities; private static String [] cities = {"London"、 "Paris"、 "NewYork"};

onCreate、 onCreateこのメソッドを開始するinitListView();

、その後は同様に、以下のonCreateその方法を行う本

プライベートボイドinitListView(){ lvCities =(リストビュー)findViewById(R.id.lvCities)。

ArrayAdapter<CharSequence>adapter=new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_checked,cities); 

    lvCities.setAdapter(adapter); 
} 

このID

アンドロイドとリストビューを持っているxmlファイルを変更します。id = "@ + ID/lvCities"

データベース・ヘルパーを使用する必要がある場合、私はあなたが必要だと思いますあなたが変更できるテーブルにデータを書き込むためにSQLiteを実装する別のクラスですが、それはstackOverflowや他のサイトで見つけることができる他のコードセット全体です。必要なものに応じて実装する方法はたくさんあります。

0

java.lang.IllegalArgumentExceptionが:列「_idは」このエラーは、カーソル内のカラム「_id」を提供していなかったことを意味し

存在しない、それは_idを使用するには、Android慣例がありますアダプター用に使用するときは、カーソルを使用してください。作業カーソルの例:お使いの場合には

String tableName = "mTable"; 
String columns[] = {"_id", "column1", "column2", "column3"}; 
cursor = db.query(table, columns, null, null, null, null, null); 

、あなたがカーソルを返すMyDBHandlerクラスのshowAllRecordsを編集()メソッド:

Cursor cursor = myDBHandler.showAllRecords(); 

あなたは_id列が含まれるようにテーブルを更新しますかエイリアスとROWID

SELECT myTable.ROWID AS _id, column1, column2, column3... from myTable 
関連する問題