2012-12-26 175 views
1

我有一个问题。 :) 在我正在开发的应用程序中,在一个部分中,我有两个选项卡,它们分别由各自的片段和列表视图运行。
现在,我需要用我在SQLite数据库中读取的数据填充这些列表。我创建了几乎所有必要的类数据库的一部分,但我被困在哪里,我需要从我提供SQLHelper类得到这个数据从数据库填充片段列表

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; 
} 

我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;去和使用该方法通过仅适用于活动扩展类和不ListFragment?什么是最好的方式来实现我想要做的事情? (附注:我非常新的节目为Android,对不起,如果我做了一些微不足道的错误:)

感谢,
Aleksa

+0

“CursorAdapter”更适合您的场景。 _custom adapter_会更好。 –

回答

0

最后我发现这是一个笨拙的入门者错误,只是mySQLiteAdapter = new DatabaseAdapter(getSherlockActivity());onViewCreated和使用阵列适配器工作得很好。

2

一个CursorAdapter是你想要的。

您只需将它挂接到从查询返回的Cursor即可。只需扩展CursorAdapter并实现newView()bindView()方法。

+0

在哪里实现这些方法和哪些类用CursorAdapter扩展(我已经exdending ListFragment,这就是为什么我问)? – murtaugh