1. 定義字串
./res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="allapp_category1_ap_item">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
<item>a5</item>
</string-array>
<string-array name="allapp_category2_ap_item">
<item>b1</item>
</string-array>
<string-array name="allapp_category3_ap_item">
<item>c1</item>
</string-array>
<string-array name="allapp_category4_ap_item">
<item>d1</item>
<item>d2</item>
<item>d3</item>
</string-array>
<string-array name="allapp_category5_ap_item">
<item></item>
</string-array>
</resources>
<string-array name="allapp_category1_ap_item">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
<item>a5</item>
</string-array>
<string-array name="allapp_category2_ap_item">
<item>b1</item>
</string-array>
<string-array name="allapp_category3_ap_item">
<item>c1</item>
</string-array>
<string-array name="allapp_category4_ap_item">
<item>d1</item>
<item>d2</item>
<item>d3</item>
</string-array>
<string-array name="allapp_category5_ap_item">
<item></item>
</string-array>
</resources>
2. 建立SQLite
./src/com/tsots/aplist/aplist.java
String tables[] = {"l_aplist"};
String fieldNames[][] = {
{ "l_id","l_content1", "l_content2", "l_content3", "l_content4", "l_content5"}
{ "l_id","l_content1", "l_content2", "l_content3", "l_content4", "l_content5"}
};
String fieldTypes[][] = {
{ "INTEGER PRIMARY KEY AUTOINCREMENT","text", "text", "text", "text", "text"}
};
int version = 1;
private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper (
this,
"SQLite_ApList.db",
null,
version,
tables,
fieldNames,
fieldTypes
);
String fieldTypes[][] = {
{ "INTEGER PRIMARY KEY AUTOINCREMENT","text", "text", "text", "text", "text"}
};
int version = 1;
private MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper (
this,
"SQLite_ApList.db",
null,
version,
tables,
fieldNames,
fieldTypes
);
3. 預設資料
./src/com/tsots/aplist/aplist.java
String[] contentValues1 = getResources().getStringArray(R.array.allapp_category1_ap_item);
String[] contentValues2 = getResources().getStringArray(R.array.allapp_category2_ap_item);
String[] contentValues3 = getResources().getStringArray(R.array.allapp_category3_ap_item);
String[] contentValues4 = getResources().getStringArray(R.array.allapp_category4_ap_item);
String[] contentValues5 = getResources().getStringArray(R.array.allapp_category5_ap_item);
String[] contentValues2 = getResources().getStringArray(R.array.allapp_category2_ap_item);
String[] contentValues3 = getResources().getStringArray(R.array.allapp_category3_ap_item);
String[] contentValues4 = getResources().getStringArray(R.array.allapp_category4_ap_item);
String[] contentValues5 = getResources().getStringArray(R.array.allapp_category5_ap_item);
String[] contentValues1={“a1”, “a2”, “a3”, “a4”, “a5”};
String[] contentValues2={“b1”};
String[] contentValues3={“c1”};
String[] contentValues4={“d1”, "d2", "d3"};
String[] contentValues5={“ ”};
String[] contentValues2={“b1”};
String[] contentValues3={“c1”};
String[] contentValues4={“d1”, "d2", "d3"};
String[] contentValues5={“ ”};
4. 找出陣列長度的最大值
./src/com/tsots/aplist/aplist.java
String[] updateCategory = {"l_content1", "l_content2", "l_content3", "l_content4", "l_content5"};
String[][] oldAllData = {contentValues1, contentValues2, contentValues3, contentValues4, contentValues5};
int k=0;
int length=0;
while(k<updateCategory.length)
{
if(oldAllData[k].length>length)
{
length = oldAllData[k].length;
}
k++;
}
oldAllData[][] =
{
{“a1”, “a2”, “a3”, “a4”, “a5”},
{“b1”},
{“c1”},
{“d1”, “d2”, “d3”},
{" "}
}
length=5
5. 將所有array重新編排為最大長度, 也就是長度一致
./src/com/tsots/aplist/aplist.java
String[] newContent1=new String[length];
for(int h=0; h<length; h++)
{
if(h > contentValues1.length-1)
{
newContent1[h] = " ";
}
else
{
newContent1[h] = contentValues1[h];
}
}
String[] newContent2 = new String[length];
for(int h=0; h<length; h++)
{
if(h > contentValues2.length-1)
{
newContent2[h] = " ";
}
else
{
newContent2[h] = contentValues2[h];
}
}
String[] newContent3 = new String[length];
for(int h=0; h<length; h++)
{
if(h > contentValues3.length-1)
{
newContent3[h] = " ";
}
else
{
newContent3[h] = contentValues3[h];
}
}
String[] newContent4 = new String[length];
for(int h=0; h<length; h++)
{
if(h > contentValues4.length-1)
{
newContent4[h] = " ";
}
else
{
newContent4[h] = contentValues4[h];
}
}
String[] newContent5 = new String[length];
for(int h=0; h<length; h++)
{
if(h > contentValues5.length-1)
{
newContent5[h] = " ";
}
else
{
newContent5[h] = contentValues5[h];
}
}
String[] newContent1={“a1”, “a2”, “a3”, “a4”, “a5”};
String[] newContent2={“b1”, “ ”, “ ”, “ ”, “ ”};
String[] newContent3={“c1”, “ ”, “ ”, “ ”, “ ”};
String[] newContent4={“d1”, “d2”, “d3”, “ “, “ “};
String[] newContent5={“ ”, “ ”, “ ”, “ ”, “ ”};
6. 存入資料庫, 因為insert()必須一次插入一整列, 所以必須將array轉換成固定y軸的形式
./src/com/tsots/aplist/aplist.java
String[][] newAllData = {newContent1,newContent2,newContent3,newContent4,newContent5};
for(int c=0;c<length;c++)
{
ArrayList<String> newArray = new ArrayList<String>();
for(int m=0;m<newAllData.length;m++)//6
{
newArray.add(newAllData[m][c]);
}
dbHelper.insert(tables[0], updateCategory, newArray.toArray());
}
when c=0
newArray.toString() = [a1,b1,c1,d1, , ]
when c=1
newArray.toString() = [a2, , ,d2, , ]
when c=2
newArray.toString() = [a3, , ,d3, , ]
when c=3
newArray.toString() = [a4, , , , , ]
when c=4
newArray.toString() = [a5, , , , , ]
沒有留言:
張貼留言