我有一系列列表视图,这些列表视图是从存储在手机上的SQLite数据库中的数据生成的。让我们说这个表看起来像这样:
__________________________________________
| ID | X | Y | Z |
|________|__________|_________|__________|
| 1 | ABC | 123 | B94 |
| 2 | ABC | 234 | C33 |
| 3 | DEF | 567 | N72 |
| 4 | DEF | 789 | K12 |
|________|__________|_________|__________|...
我想从列'X'创建一个列表视图,点击后,打开“Y”列中与新列表视图中的行相关联的数据。棘手的部分是我想在列表视图中将每列中的相同项目组合在一起。在这个例子中,第一个listview只有两个项目“ABC; DEF”,当点击“ABC”时,它将在下一个listview中显示“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
}
}
}
所以这基本上只是将一堆信息存储在一个表中,该表将游标加载到listview中。如果有人可以帮助我使用OnItemClickListener或将列项分组在一起,那就太棒了!
答案 0 :(得分:1)
您必须设置一个新的listadaper,将结果限制为ABC
例如:
Cursor c = sqlLiteDatabase.query(MYDATABASE_TABLE,
new String[] {KEY_ID,COLUMN_Y},
COLUMN_X + "=" + ITEM_IN_COLUMN_X_CLICKED,
null,null,null);
密钥是查询中的where
,您想要查看所有项目,点击项目即。 COLUMN_X + "=" + ITEM_IN_COLUMN_X_CLICKED
。
因此,如果您点击ABC
,新列表会在column y
中ABC
column x
中显示{{1}}中的所有内容