Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CursorCountingAdapter extends CursorAdapter {
- private int layout;
- public CursorCountingAdapter(Context context, int layout, Cursor cursor, int flag) {
- super(context, cursor, flag);
- this.layout = layout;
- }
- public static class ViewHolder {
- public TextView ingr;
- public TextView val;
- public TextView uni;
- public CheckBox myCheck;
- public ViewHolder(View view) {
- ingr = (TextView) view.findViewById(R.id.countTvIngr);
- val = (TextView) view.findViewById(R.id.countTvVal);
- uni = (TextView) view.findViewById(R.id.countTvUni);
- myCheck = (CheckBox) view.findViewById(R.id.myCheck);
- }
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- Log.d("getViewTest", "new view");
- LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(layout, parent, false);
- ViewHolder viewHolder = new ViewHolder(view);
- view.setTag(viewHolder);
- return view;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- Log.d("valuecheck","bindviewcalled");
- ViewHolder holder = (ViewHolder) view.getTag();
- String ingredients = cursor.getString(cursor.getColumnIndex(DB.COLUMN_INGR));
- String values = cursor.getString(cursor.getColumnIndex(DB.COLUMN_VAL));
- String units = cursor.getString(cursor.getColumnIndex(DB.COLUMN_UNI));
- String check = cursor.getString(cursor.getColumnIndex((DB.COLUMN_CHEKBOX)));
- int _ID = cursor.getInt(cursor.getColumnIndex(DB.COLUMN_ID));
- holder.ingr.setText(ingredients);
- holder.val.setText(values);
- holder.uni.setText(units);
- holder.myCheck.setFocusable(false);
- if (check.equals("1")){
- holder.myCheck.setChecked(true);
- }
- else {
- holder.myCheck.setChecked(false);
- }
- holder.myCheck.setTag(new Model(_ID, ingredients, values, units, check));
- }
- }
- public class Model {
- long id;
- String mIngr;
- String mVal;
- String mUni;
- boolean mBox;
- public Model(long id, String ingr, String val, String uni, String box) {
- this.id = id;
- this.mIngr = ingr;
- this.mVal = val;
- this.mUni = uni;
- this.mBox = (box.equals("1"))? true:false;
- }
- }
- public class CountingFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
- private static final int CM_DELETE_ID = 1;
- public static final int COUNT_LOADER = 2;
- ListView lvData;
- DB db;
- CursorCountingAdapter scAdapter;
- CheckBox mCheckBox;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.fragment_counting, container, false);
- lvData = (ListView)v.findViewById(R.id.lvDataCount);
- mCheckBox = (CheckBox)v.findViewById(R.id.myCheck);
- lvData.setItemsCanFocus(false);
- db = new DB(getActivity());
- db.open();
- // формируем столбцы сопоставления
- String[] from = new String[] {DB.COLUMN_INGR, DB.COLUMN_VAL, DB.COLUMN_UNI};
- final int[] to = new int[] {R.id.countTvIngr, R.id.countTvVal, R.id.countTvUni};
- scAdapter = new CursorCountingAdapter(getActivity(), null);
- lvData.setAdapter(scAdapter);
- registerForContextMenu(lvData);
- getActivity().getSupportLoaderManager().initLoader(COUNT_LOADER, null, this);
- return v;
- }
- @Override
- public void onStart() {
- super.onStart();
- getActivity().getSupportLoaderManager().restartLoader(COUNT_LOADER, null, this);
- }
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenu.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) {
- // получаем из пункта контекстного меню данные по пункту списка
- AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item
- .getMenuInfo();
- // извлекаем id записи и удаляем соответствующую запись в БД
- db.delRec(acmi.id);
- // получаем новый курсор с данными
- getActivity().getSupportLoaderManager().getLoader(COUNT_LOADER).forceLoad();
- return true;
- }
- return super.onContextItemSelected(item);
- }
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- db.close();
- }
- @Override
- public Loader<Cursor> onCreateLoader(int id, Bundle args) {
- switch (id){
- case COUNT_LOADER:
- String name = getArguments().getString("name"); //строка для получения аргументов создается в onCreateLoader
- return new CursorLoaderCount(getActivity(), db, id, name);
- }
- return null;
- }
- @Override
- public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
- scAdapter.swapCursor(data);
- }
- @Override
- public void onLoaderReset(Loader loader) {
- scAdapter.swapCursor(null);
- }
- static class CursorLoaderCount extends CursorLoader {
- Cursor cursor;
- final int LoaderId;
- DB db;
- String name;
- public CursorLoaderCount(Context context, DB db, int id, String name) {
- super(context);
- this.db = db;
- LoaderId = id;
- this.name = name;
- }
- @Override
- public Cursor loadInBackground() {
- switch (LoaderId){
- case COUNT_LOADER:
- cursor = db.getIngrNew(name);
- break;
- }
- return cursor;
- }
- }
- }
- public class DB {
- private static final String DB_NAME = "mealsDbb";
- private static final int DB_VERSION = 1;
- private static final String DB_TABLE = "myMeals";
- public static final String COLUMN_ID = "_id";
- public static final String COLUMN_MEAL = "mealing";
- public static final String COLUMN_IMG = "img";
- public static final String COLUMN_INGR = "ingr";
- public static final String COLUMN_VAL = "val";
- public static final String COLUMN_UNI = "uni";
- public static final String COLUMN_IMGV3 = "img3";
- public static final String COLUMN_CHEKBOX = "myCheck";
- //private static String DB_DELETE = "drop table " + DB_TABLE + ");";
- private static final String DB_CREATE =
- "create table " + DB_TABLE + "(" +
- COLUMN_ID + " integer primary key autoincrement, " +
- COLUMN_IMG + " integer, " +
- COLUMN_MEAL + " text, " +
- COLUMN_IMGV3 + " integer, " +
- COLUMN_INGR + " text, " +
- COLUMN_VAL + " text, " +
- COLUMN_UNI + " text, " +
- COLUMN_CHEKBOX + " integer" +
- ");";
- private final Context mCtx;
- private DBHelper mDBHelper;
- private SQLiteDatabase mDB;
- public DB(Context ctx) {
- mCtx = ctx;
- }
- public void checkedTrue (int position){
- mDB.execSQL("UPDATE " + DB_TABLE + " SET " + COLUMN_CHEKBOX + " = '1' WHERE _id = " + position);
- }
- public void checkedFalse (int position){
- mDB.execSQL("UPDATE " + DB_TABLE + " SET " + COLUMN_CHEKBOX + " = '0' WHERE _id = " + position);
- }
- // открыть подключение
- public void open() {
- mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
- mDB = mDBHelper.getWritableDatabase();
- }
- // закрыть подключение
- public void close() {
- if (mDBHelper != null) mDBHelper.close();
- }
- // получить все данные из таблицы DB_TABLE
- public Cursor getAllData() {
- return mDB.query(DB_TABLE, new String[]{COLUMN_ID, DB.COLUMN_MEAL, DB.COLUMN_IMG, DB.COLUMN_INGR, DB.COLUMN_VAL, DB.COLUMN_UNI}, null, null, null, null, null, null);
- }
- public Cursor getMeal () {
- //return mDB.query(DB_TABLE, new String[]{COLUMN_ID, COLUMN_MEAL}, null, null, null, null, null);
- return mDB.query(DB_TABLE, new String[]{COLUMN_ID, COLUMN_MEAL, COLUMN_IMGV3}, null, null, COLUMN_MEAL, null, null);
- }
- public Cursor getIngr () {
- return mDB.query(DB_TABLE, new String[]{COLUMN_ID, DB.COLUMN_MEAL, DB.COLUMN_INGR, DB.COLUMN_VAL, DB.COLUMN_UNI}, null, null, /*COLUMN_MEAL + " LIKE ?", Arrays.toString(new String[]{String.valueOf(name)}),*/ null, null, null);
- }
- public Cursor getIngrNew (String name) {
- return mDB.query(DB_TABLE, new String[]{COLUMN_ID, DB.COLUMN_INGR, DB.COLUMN_VAL, DB.COLUMN_UNI}, DB.COLUMN_MEAL + " = ?", new String[]{name}, null, null, null);
- }
- // добавить запись в DB_TABLE
- public void addRec(int img, int img3, String mealing, String ingr, String val, String uni, int checkbox) {
- ContentValues cv = new ContentValues();
- cv.put(COLUMN_UNI, uni);
- cv.put(COLUMN_VAL, val);
- cv.put(COLUMN_INGR, ingr);
- cv.put(COLUMN_MEAL, mealing);
- cv.put(COLUMN_IMG, img);
- cv.put(COLUMN_IMGV3, img3);
- cv.put(COLUMN_CHEKBOX, checkbox);
- mDB.insert(DB_TABLE, null, cv);
- }
- // удалить запись из DB_TABLE
- public void delRec(long id) {
- mDB.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
- }
- // класс по созданию и управлению БД
- public class DBHelper extends SQLiteOpenHelper {
- public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
- int version) {
- super(context, name, factory, version);
- }
- // создаем и заполняем БД
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DB_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
- }
- public class CheckableRelativeLayout extends RelativeLayout implements
- Checkable {
- private boolean isChecked;
- private List<Checkable> checkableViews;
- public CheckableRelativeLayout(Context context, AttributeSet attrs,
- int defStyle) {
- super(context, attrs, defStyle);
- initialise(attrs);
- }
- public CheckableRelativeLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialise(attrs);
- }
- public CheckableRelativeLayout(Context context, int checkableId) {
- super(context);
- initialise(null);
- }
- /*
- * @see android.widget.Checkable#isChecked()
- */
- public boolean isChecked() {
- return isChecked;
- }
- /*
- * @see android.widget.Checkable#setChecked(boolean)
- */
- public void setChecked(boolean isChecked) {
- this.isChecked = isChecked;
- for (Checkable c : checkableViews) {
- c.setChecked(isChecked);
- }
- }
- /*
- * @see android.widget.Checkable#toggle()
- */
- public void toggle() {
- this.isChecked = !this.isChecked;
- for (Checkable c : checkableViews) {
- c.toggle();
- }
- }
- @Override
- protected void onFinishInflate() {
- super.onFinishInflate();
- final int childCount = this.getChildCount();
- for (int i = 0; i < childCount; ++i) {
- findCheckableChildren(this.getChildAt(i));
- }
- }
- /**
- * Read the custom XML attributes
- */
- private void initialise(AttributeSet attrs) {
- this.isChecked = false;
- this.checkableViews = new ArrayList<Checkable>(5);
- }
- /**
- * Add to our checkable list all the children of the view that implement the
- * interface Checkable
- */
- private void findCheckableChildren(View v) {
- if (v instanceof Checkable) {
- this.checkableViews.add((Checkable) v);
- }
- if (v instanceof ViewGroup) {
- final ViewGroup vg = (ViewGroup) v;
- final int childCount = vg.getChildCount();
- for (int i = 0; i < childCount; ++i) {
- findCheckableChildren(vg.getChildAt(i));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement