0

カーソルを使用して SQLite データベースからデータを読み取り、アダプタを使用して listView に表示しています。これは問題なく動作しますが、listView に表示するデータの量を減らしたいと考えています。現時点では、次のように表示されます。

John Smith, 25, Moday, Consultation, Dr. Harley
Jane Doe, 41, Wednesday, Surgery, Dr. Pope

表示したいのは次のとおりです。

John Smith, 25, Mo, Con, Harley
Jane Doe, 41, We, Sur, Pope

基本的に、3 つの文字列を解析したいと考えています。問題は、カーソル アダプターがデータベースの列をコンストラクターの文字列配列として受け取るため、どこで解析操作を実行すればよいかわかりません。さまざまなオプションを試してみましたが、認識できない列 ID エラーやその他の実行時エラーが発生しています。誰かが私を正しい方向に向けることができますか?

アダプターが作成されるメソッド:

private void fillList() {
        Cursor c = db.getApts();
        startManagingCursor(c);

        String[] from = new String[] {ModuleDB.KEY_AptCode,
                          ModuleDB.KEY_AptName,
                          ModuleDB.KEY_AptAge,
                          ModuleDB.KEY_AptDay,
                          ModuleDB.KEY_AptType,
                                  ModuleDB.KEY_AptDoc};
        int[] to = new int[] {R.id.Aptcode_entry,
                      R.id.AptName_entry,
                      R.id.AptAge_entry,
                      R.id.Aptday_entry,
                              R.id.Apttype_entry,
                              R.id.Aptdoc_entry};

        SimpleCursorAdapter aptAdapter = 
                new SimpleCursorAdapter(this, R.layout.apt_entry, c, from, to);
        setListAdapter(aptAdapter);

    }
4

3 に答える 3

3

1.) アクティビティを実装する - ViewBinder

2.) 列を一致させ、部分文字列を使用する

public class YourActivity extends Activity
    implements SimpleCursorAdapter.ViewBinder {

   adapter.setViewBinder(this); //Put this line after your list creation and setlistAdapter


    @Override
    public boolean setViewValue(View view, Cursor cursor, int column) {
     //Showing for day, similarly for others
        int dayColumn = cursor.getColumnIndex(your day column name in quotes);
        if (column == dayColumn ) {
            String dayString = cursor.getString(dayColumn );
            ((TextView)view).setText(bodyString.subString(0, 3));

            return true; // Return true to show you've handled this column
        }
        return false;
    }

}

また、@Simon は正しいです - カーソル アダプタを拡張するカスタム アダプタを使用することは、要件がさらに進化した場合に後で変更する自由が増えるため、常に優れています。私の頭の上から、カスタムアダプターを使用して素敵なリストを作成する方法の例を次に示します- http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text /

于 2012-11-07T15:00:00.260 に答える
1

単純なアダプタでtoString()をオーバーライドできるとは思わないが、おそらくこれは役立つだろうか?

SimpleCursorAdapter aptAdapter= new SimpleCursorAdapter(this, R.layout.apt_entry, c, from, to);

 CursorToStringConverter stringConverter = new CursorToStringConverter() {    
    @Override
    public CharSequence convertToString(Cursor cursor) {
       return "Hello listview"; // whatever string you want to build using cursor.getString() etc
    }
 }; 

 aptAdapter.setCursorToStringConverter(stringConverter);

[編集]ドキュメントを確認したところ、SimpleCursorAdapterにはtoString()メソッドがなく、スーパークラスもありません。

于 2012-11-07T14:51:25.040 に答える
1

使用するアダプターはわかりませんが、すべてのデータが単一の行として表示されていると仮定すると、そのアダプターを拡張してtoString()メソッドをオーバーライドします。

于 2012-11-07T14:41:42.760 に答える