我有一系列的listview是從存儲在手機上的SQLite數據庫中的數據中生成的。比方說表是這樣的:來自SQLite的組列項(Android)
__________________________________________
| ID | X | Y | Z |
|________|__________|_________|__________|
| 1 | ABC | 123 | B94 |
| 2 | ABC | 234 | C33 |
| 3 | DEF | 567 | N72 |
| 4 | DEF | 789 | K12 |
|________|__________|_________|__________|...
我想創建列「X」的列表視圖,並且點擊時,打開了一個與它相關聯的行中的一個新的列表視圖中列「Y」的數據。棘手的部分是我想在列表視圖中將每列中的相同項目分組在一起。在這個例子中,第一個列表視圖只有兩個項目「ABC; DEF」,當點擊「ABC」時,它將在下一個列表視圖中顯示「123; 234」項目。
繼承人我的代碼(是的,我修改了其中大部分來自教程!):
listviewactivity.java
public class listviewactivity extends Activity {
private SQLiteAdapter mySQLiteAdapter;
ListView listContent;
SimpleCursorAdapter cursorAdapter;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.expenses);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2};
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2};
cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mySQLiteAdapter.close();
}
}
SQLiteAdapter.java
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "MY_DATABASE";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";
public static final String KEY_CONTENT3 = "Content3";
public static final String KEY_CONTENT4 = "Content4";
public static final String KEY_CONTENT5 = "Content5";
public static final String KEY_CONTENT6 = "Content6";
public static final String KEY_CONTENT7 = "Content7";
public static final String KEY_CONTENT8 = "Content8";
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT1 + " text not null, "
+ KEY_CONTENT2 + " text not null,"
+ KEY_CONTENT3 + " text not null, "
+ KEY_CONTENT4 + " text not null,"
+ KEY_CONTENT5 + " text not null, "
+ KEY_CONTENT6 + " text not null,"
+ KEY_CONTENT7 + " text not null,"
+ KEY_CONTENT8 + " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c){
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content1, String content2, String content3,
String content4, String content5, String content6,
String content7, String content8){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT1, content1);
contentValues.put(KEY_CONTENT2, content2);
contentValues.put(KEY_CONTENT3, content3);
contentValues.put(KEY_CONTENT4, content4);
contentValues.put(KEY_CONTENT5, content5);
contentValues.put(KEY_CONTENT6, content6);
contentValues.put(KEY_CONTENT7, content7);
contentValues.put(KEY_CONTENT8, content8);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2, KEY_CONTENT3,
KEY_CONTENT4, KEY_CONTENT5, KEY_CONTENT6,
KEY_CONTENT7, KEY_CONTENT8};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null, null);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
所以這只是基本上將一堆信息存儲在一個將光標加載到al中的表中istview。如果任何人都可以使用我的OnItemClickListener或將列項目組合在一起,那將會非常棒!
此作品謝謝你,我只是需要一種方法將類似的物品欄吧! – 725623452362 2012-02-04 16:51:45