我正在尝试通过为表中的每列插入标记来创建多个表,然后根据标记名称对它们进行排序
食品苹果
tags = movies
我知道如何在常规方法中使用一些for循环来执行此操作:
public ArrayList<Items> getall(String tag ,boolean first) {
open();
ArrayList<Items> list = new ArrayList<Items>();
Cursor cur = myDb.query(TABLE_NOTES, null, null, null, null, null, null, null);
while (cur.moveToNext()) {
if (first) {
Items data = new Items(cur.getString(1),cur.getString(2),cur.getFloat(3),cur.getString(4));
list.add(data);
}
if(cur.getString(2).equals(tag)&&!first){
Items data = new Items(cur.getString(1),cur.getString(2),cur.getFloat(3),cur.getString(4));
list.add(data);
}
}
close();
return list;
}
但我怎么能用ContentProvider呢? 公共类StatusProvider扩展了ContentProvider {
public static final String AUTH = "content://com.example.mulitplytables.provider";
public static final Uri CONTENT_URI = Uri.parse(AUTH);
SQLiteDatabase db;
DbHandler myDb;
@Override
public boolean onCreate() {
myDb = new DbHandler(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
db = myDb.getReadableDatabase();
Cursor corsor = db.query(DbHandler.TABLE, projection, selection, selectionArgs, null, null, null);
return corsor;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = myDb.getWritableDatabase();
long id = db.insertWithOnConflict(DbHandler.TABLE, null, values,SQLiteDatabase.CONFLICT_IGNORE);
if(id != -1){
uri = Uri.withAppendedPath(uri, Long.toString(id));
}
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
db = myDb.getWritableDatabase();
int result = db.delete(DbHandler.TABLE, selection, selectionArgs);
return result;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
db = myDb.getWritableDatabase();
int result = db.updateWithOnConflict(DbHandler.TABLE, values,selection, selectionArgs, SQLiteDatabase.CONFLICT_IGNORE);
return result;
}
}
公共类DbHandler扩展了SQLiteOpenHelper {
public static final String DBName = "noteDb.db";
public static final int Version = 1;
public static final String TABLE = "Notes";
public static final String ID = "_id";
public static final String NOTES = "myNotes";
public static final String TAGS = "myTags";
public static final String SET1 = "set1";
public static final String SET2 = "set2";
private static final String CREATE_TABLE_NOTES = "CREATE TABLE "
+ TABLE + "(" + ID + " INTEGER PRIMARY KEY," + NOTES
+ " TEXT,"+TAGS + " TEXT,"+SET1+ " REAL,"+SET2+" TEXT)";
public DbHandler(Context context) {
super(context, DBName, null, Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_NOTES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
onCreate(db);
}
}
编辑:
static int MY_LOADER_ID = 1;
DbHandler myDb;
ListView lv;
CustomCursorAdapter adapter;
ArrayList<Notes> list = new ArrayList<Notes>();
Spinner sp;
String[] spData;
ArrayList<String> sortedList;
ArrayAdapter<String> adapterSpinner;
String category = "";
String note = "";
float set1 = 0;
String set2 = "";
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
lv.setOnItemClickListener(this);
cursor = getContentResolver().query(StatusProvider.CONTENT_URI, null, null, null, null);
adapter = new CustomCursorAdapter(this, cursor, 0);
lv.setAdapter(adapter);
getLoaderManager().initLoader(MY_LOADER_ID, null, this);
sp = (Spinner) findViewById(R.id.spinner1);
adapterSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, setSpinnerTags());
sp.setAdapter(adapterSpinner);
sp.setOnItemSelectedListener(this);
}
//Set the tags names on the spinner
private String[] setSpinnerTags() {
ArrayList<String> result = new ArrayList<String>();
while (cursor.moveToNext()) {
String tagGet = cursor.getString(cursor.getColumnIndex(DbHandler.TAGS));
if(!result.contains(tagGet)){
result.add(tagGet);
}
}
spData = new String[result.size()];
for (int i = 0; i < result.size(); i++) {
spData[i] = result.get(i);
}
return spData;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
//HERE is the title of th tags
String theTitle = spData[position];
//?????? What i do need the write here?
switch (position) {
case 0:
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(this, StatusProvider.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
public void refreshList(){
getLoaderManager().restartLoader(MY_LOADER_ID, null, this);
}
}