2017-10-30 3 views
0

authorというテーブルを持つSQLiteデータベースがあります。 idnameの値を引き出し、MainActivityに渡そうとしています。idが変数として格納され、nameがListViewに表示されます。カーソル内の複数の変数を期待通りに動作させない設定

しかし、これは、ログ(と私のListView)がどのように見えるかです:

[[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected]] 

私はAuthorListアレイにidnameの両方を追加することだけにはどちらを指定していないよので、問題があることがわかりますListViewに表示します。しかし、どうすればいいのですか?

DatabaseHelperクラスからの抜粋:

public List<String> getAllAuthors() { 

     List authorList = new ArrayList(); 
    // Select all query 
    String selectQuery = "SELECT * FROM " + AUTHORS + " ORDER BY name_alphabetic"; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Author author = new Author(); 
      author.setID(Integer.parseInt(cursor.getString(0))); 
      author.setName(cursor.getString(1)); 
      authorList.add(author); 
     } while (cursor.moveToNext()); 
    } 

    // return author list 
    return authorList; 
} 

Authorクラス:MainActivityから

public class Author { 

    int id; 
    String name; 
    String name_alphabetic; 

    public Author() { 

    } 

    public Author(int id, String name, String name_alphabetic) { 
     this.id = id; 
     this.name = name; 
     this.name_alphabetic = name_alphabetic; 
    } 

    // getters 

    public int getID() { 
     return this.id; 
    } 

    public String getName() { 
     return this.name; 
    } 

    public String getNameAlphabetic() { 
     return this.name_alphabetic; 
    } 

    // setters 

    public void setID(int id) { 
     this.id = id; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setNameAlphabetic(String name_alphabetic) { 
     this.name_alphabetic = name_alphabetic; 
    } 
} 

スニペット:

 // connect authorsListView variable to XML ListView 
     authorsListView = (ListView) findViewById(R.id.authors_list_view); 

     // create new database helper 
     DatabaseHelper db = new DatabaseHelper(this); 

     // create new list through getAllAuthors method (in DatabaseHelper class) 
     List authorList = db.getAllAuthors(); 

     Log.i("authors", authorList.toString()); 

     // create new Array Adapter 
     ArrayAdapter<String> arrayAdapter = 
       new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, authorList); 

     // link ListView and Array Adapter 
     authorsListView.setAdapter(arrayAdapter); 

EDIT:

下記のコメントからthis threadを使用して問題を解決しました。あなたが本当にあなたのAuthorクラスを使用していない

@Override 
    public String toString() { 
    return name; 
    } 
+0

シンプルリストアイテムレイアウトです。おそらく 'toString()'に依存しないように、あなたのリストアイテムの適切なレイアウトをコーディングしておくべきでしょう。 –

+0

ジェネリックを使用してください。 'List authorList = new ArrayList()'を 'List authorList = new ArrayList <>()'に置き換えた場合、コンパイル時に問題が見えます。 – PPartisan

+0

方法1:著者リストから、作成者名だけでなく新しいリストを作成し、アレイアダプタに設定します。方法2:ArrayAdapterを拡張してカスタムのAdapterクラスを作成します。 – Sackurise

答えて

0

Objectクラスから継承toString()メソッドをオーバーライドするために私Authorクラスにこのコードを追加すると、トリックをしました。

変更あなたのListList<Author>に:

public List<Author> getAllAuthors() { 

    List<Author> authorList = new ArrayList<>(); 
    // Select all query 
    String selectQuery = "SELECT * FROM " + AUTHORS + " ORDER BY name_alphabetic"; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Author author = new Author(); 
      author.setID(Integer.parseInt(cursor.getString(0))); 
      author.setName(cursor.getString(1)); 
      authorList.add(author); 
     } while (cursor.moveToNext()); 
    } 

    // return author list 
    return authorList; 
} 

その後MainActivity.java変更ArrayAdapterに沿ったもの:

ArrayAdapter<Author> arrayAdapter = 
      new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, authorList); 

AuthortoString()を返すことを確認してください、私はあなたが次の操作を行うことを示唆していますあなたが望むようにフォーマットされた文字列。

関連する問題