1

質問があります。:)私が取り組んでいるアプリでは、ある部分に2つのタブがあり、どちらもそれぞれのフラグメントとそれぞれのリストビューによって実行されます。
次に、SQLiteデータベースで読み取っているデータをこれらのリストに入力する必要があります。DB部分に必要なほとんどすべてのクラスを作成しましたが、このデータを取得する必要がある部分で立ち往生しています

 public List<String> getAllRockBands() {
     List<String> bandList = new ArrayList<String>();

     String selection = BAND_GENRE + "=1";
     String orderBy = BAND_NAME+" ASC";

     SQLiteDatabase db = sqLiteHelper.getReadableDatabase();
     Cursor cursor = db.query(BANDDATABASE_TABLE,new String[]{BAND_NAME}, selection,  null, null, null, orderBy);

     int index = cursor.getColumnIndex(BAND_NAME);

     if (cursor.moveToFirst()) {
         do {
          String bandName = cursor.getString(index);
              bandList.add(bandName);
         } while (cursor.moveToNext());
     }

     return bandList;
 }

sqlhelperクラスからlistfragment拡張クラスへ。そのためのコードは現在次のとおりです。

public class RockAllFragment extends SherlockListFragment {

private String[] bandList;

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {

     return inflater.inflate(R.layout.my_list, null);
 }

  @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
 }

 @Override
 public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getSherlockActivity(),   android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, bandList));
 }
}

private BandDatabaseAdapter mySQLiteAdapter;このメソッドを使用して、Activity拡張クラスでのみ機能し、ListFragmentでは機能しませんか?私がやりたいことを達成するための最良の方法は何でしょうか?(PS私はAndroidのプログラミングにかなり慣れていません。些細な間違いをした場合は、申し訳ありません:)

ありがとう、
アレクサ

4

2 に答える 2

1

CursorAdapterはあなたが望むものです。

クエリから返されたカーソルにフックするだけです。CursorAdapterを拡張し、newView()andbindView()メソッドを実装するだけです。

于 2012-12-26T04:49:25.790 に答える
0

最後に、それは不器用な初心者の間違いであることがわかりました.atを使用して、アレイアダプターを使用するだけで問題なく機能しましたmySQLiteAdapter = new DatabaseAdapter(getSherlockActivity());onViewCreated

于 2013-01-18T16:34:38.140 に答える