Android:如何使用数据库中的值动态创建锯齿状数组

时间:2013-12-12 05:22:08

标签: java android listview jagged-arrays sectionheader

我正在使用Android Amazing ListView创建带有粘性标头的自定义ListView。在此库中,数据正在Data.java类中设置。我需要做的是,我必须传递我在本地创建的数据库中的标头和列表数据的值。我能够设置标题,但是,我无法根据这些标题传递列表数据。填充的列表数据以下列格式传递:

Composer[][] composerss = {
                    {
                        new Composer("Thomas Tallis"),
                        new Composer("Josquin Des Prez"),
                        new Composer("Pierre de La Rue"),
                    },
                    {
                        new Composer("Johann Sebastian Bach"),
                        new Composer("George Frideric Handel"),
                        new Composer("Antonio Vivaldi"),
                        new Composer("George Philipp Telemann"),
                    },
                     {
                        new Composer("Franz Joseph Haydn"),
                        new Composer("Wolfgang Amadeus Mozart"),
                        new Composer("Barbara of Portugal"),
                        new Composer("Frederick the Great"),
                        new Composer("John Stanley"),
                        new Composer("Luise Adelgunda Gottsched"),
                        new Composer("Johann Ludwig Krebs"),
                        new Composer("Carl Philipp Emanuel Bach"),
                        new Composer("Christoph Willibald Gluck"),
                        new Composer("Gottfried August Homilius"),
                    },
                    {
                        new Composer("Ludwig van Beethoven"),
                        new Composer("Fernando Sor"),
                        new Composer("Johann Strauss I"),
                    },
                    {
                        new Composer("Ludwig van Beethoven"),
                        new Composer("Fernando Sor"),
                        new Composer("Johann Strauss I"),
                    },
            };

我需要知道,如何从我从数据库中检索的数据创建这样的锯齿状数组。我能够根据标题获取数据,但我需要以这种格式传递数据,以便相应地排列标题下的数据。希望尽快得到帮助。

1 个答案:

答案 0 :(得分:0)

要从数据库中获取值并创建锯齿状数组,您可以执行以下操作:

获取一个ArrayList并将所有索引存储在其中。

for(int i = 0; i<myIndexList.size(); i++)
{
    String[] s = null;
    Log.e("", "current pos "+i);
    Cursor mCur2 = mDb.sGetMySectionListData(myIndexList.get(i));
    if(mCur2.getCount()>0)
        {
            s = new String[mCur2.getCount()];
            mCur2.moveToFirst();
            do
                {
                    s[mCur2.getPosition()] = mCur2.getString(mCur2.getColumnIndex("section_data")));
                }
            while(mCur2.moveToNext());
        }
    mCur2.close();
    mGenerateString(s, i, myIndexList.size());
}

然后您可以使用以下方法生成锯齿状数组

private void mGenerateString(String[] mCurrString, int pos, int size)
{
    mStr[pos] = mCurrString;
    Log.e("", "string array : "+mStr[pos]);
}