Код: Выделить всё
public class ListActivity extends FragmentActivity implements LoaderCallbacks<Cursor> {
final static String LOG_TAG1 = "ListLogs";
private static final int CM_DELETE_ID = 1;
ListView lvData;
SimpleCursorAdapter scAdapter;
DB db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main_view);
db = new DB(this);
db.open();
Log.d(LOG_TAG1, "DB open");
String[] from = new String[] {DB.COLUMN_NAME_P, DB.COLUMN_DISTRICT_P, DB.COLUMN_SALARY_POS};
int[] to = new int[] {R.id.name_textView, R.id.position_textView, R.id.salary_textView};
scAdapter = new SimpleCursorAdapter (ListActivity.this, R.layout.item, null, from, to, 0);
lvData = (ListView) findViewById (R.id.data_listView);
lvData.setAdapter(scAdapter);
registerForContextMenu(lvData);
getSupportLoaderManager().initLoader(0, null, this);
//getSupportLoaderManager().getLoader(0).forceLoad();
}
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
}
public boolean onContextItemSelected (MenuItem item) {
if (item.getItemId() == CM_DELETE_ID) {
AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) item.getMenuInfo();
String ida = acmi.toString();
db.delRec(ida);
getSupportLoaderManager().getLoader(0).forceLoad();
return true;
}
return super.onContextItemSelected(item);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
// TODO Auto-generated method stub
return new MyCursorLoader (ListActivity.this, db);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// TODO Auto-generated method stub
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
}
static class MyCursorLoader extends CursorLoader {
DB db;
public MyCursorLoader (Context context, DB db) {
super (context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.grpSalaryAll();
return cursor;
}
}
}
Код: Выделить всё
public class DB {
final String LOG_TAG = "MyLog";
private static final String DB_NAME = "TestDb";
private static final int DB_VERSION = 2;
private static final String DB_TABLE_P = "people";
private static final String DB_TABLE_POS = "position";
public static final String COLUMN_ID_P = "_id";
public final static String COLUMN_NAME_P = "name";
public final static String COLUMN_DISTRICT_P = "district";
private static final String COLUMN_POSID_P = "posid";
public static final String COLUMN_ID_POS = "_id";
public static final String COLUMN_NAME_POS = "name";
public final static String COLUMN_SALARY_POS = "salary";
private static final String DB_CREATE_P =
"create table " + DB_TABLE_P + "(" +
COLUMN_ID_P + " integer primary key autoincrement, " +
COLUMN_NAME_P + " text," +
COLUMN_DISTRICT_P + " text, " +
COLUMN_POSID_P + " integer" +
");";
private static final String DB_CREATE_POS =
"create table " + DB_TABLE_POS + "(" +
COLUMN_ID_POS + " integer primary key, " +
COLUMN_NAME_POS + " text, " +
COLUMN_SALARY_POS + " integer" +
");";
private final Context mCtx;
private DBHelper mDBHelper;
private SQLiteDatabase mDB;
int[] position_id = {1, 2, 3, 4};
String[] position_name = {"Director", "Programmer", "Buhgalter", "Security"};
int[] position_salary = {15000, 13000, 10000, 8000};
String tablex = "people as PL inner join position as PS on PL.posid = PS._id";
String []columnsx = new String[] {"PL.name as Name","PS._name as Position", "_salary as Salary"};
public DB (Context ctx) {
mCtx = ctx;
}
public void open() {
mDBHelper = new DBHelper (mCtx, DB_NAME, null, DB_VERSION);
mDB = mDBHelper.getWritableDatabase();
}
public void close() {
if (mDBHelper != null) mDBHelper.close();
}
public Cursor getAllData() {
return mDB.query(DB_TABLE_P, null, null, null, null, null, null);
}
public Cursor getAllDataPOS() {
return mDB.query(DB_TABLE_POS, null, null, null, null, null, null);
}
public void addRec (String _name, String _district, String _posid) {
ContentValues cvRow = new ContentValues();
cvRow.put(COLUMN_NAME_P, _name);
cvRow.put(COLUMN_DISTRICT_P, _district);
cvRow.put(COLUMN_POSID_P, _posid);
long rowId = mDB.insert(DB_TABLE_P, null, cvRow);
Log.d(LOG_TAG, "Create row ID = " + rowId);
}
public void delRec (String _id) {
mDB.delete(DB_TABLE_P, COLUMN_ID_P + "=" + _id, null);
}
public void updRec (String _uid, String _name, String _district, String _posid) {
ContentValues cvRow = new ContentValues();
cvRow.put(COLUMN_NAME_P, _name);
cvRow.put(COLUMN_DISTRICT_P, _district);
cvRow.put(COLUMN_POSID_P, _posid);
mDB.update(DB_TABLE_P, cvRow, "_id = ?", new String[] {_uid});
}
public void delTbl () {
mDB.delete(DB_TABLE_P, null, null);
}
public void grpSalary (String _salary) {
String sqlRawQuery = "select PL.name as Name, PS.name as Position, salary as Salary"
+ "from people as PL"
+ "inner join position as PS"
+ "on PL.posid = PS._id"
+ "where _salary > ?";
mDB.rawQuery(sqlRawQuery, new String[] {_salary});
}
public Cursor grpSalaryAll () {
String table = "people as PL inner join position as PS on PL.posid = PS._id";
String columns[] = { "PL._id as IDP", "PL.name as Name", "PS._id as IDPOS", "PS.name as Position", "salary as Salary"};
return mDB.query(table, columns, null, null, null, null, null);
}
public void grpDistrict () {
String groupBy = "district";
mDB.query(tablex, columnsx, null, null, groupBy, null, null);
}
public void sorting(String _orderBy) {
mDB.query(tablex, columnsx, null, null, null, null, _orderBy);
}
private class DBHelper extends SQLiteOpenHelper {
public DBHelper ( Context context, String name, CursorFactory factory, int version) {
super (context, name, factory, version);
}
@Override
public void onCreate (SQLiteDatabase db) {
db.execSQL(DB_CREATE_P);
db.execSQL(DB_CREATE_POS);
Log.d(LOG_TAG, "Create database complete...");
ContentValues cvRow = new ContentValues();
for (int i = 0; i < position_id.length; i++) {
cvRow.clear();
cvRow.put(COLUMN_ID_POS, position_id[i]);
cvRow.put(COLUMN_NAME_POS, position_name[i]);
cvRow.put(COLUMN_SALARY_POS, position_salary[i]);
db.insert("position", null, cvRow);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}