Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. public class CursorCountingAdapter extends CursorAdapter {
  2.  
  3. private int layout;
  4.  
  5.  
  6. public CursorCountingAdapter(Context context, int layout, Cursor cursor, int flag) {
  7. super(context, cursor, flag);
  8. this.layout = layout;
  9. }
  10.  
  11. public static class ViewHolder {
  12. public TextView ingr;
  13. public TextView val;
  14. public TextView uni;
  15. public CheckBox myCheck;
  16.  
  17. public ViewHolder(View view) {
  18. ingr = (TextView) view.findViewById(R.id.countTvIngr);
  19. val = (TextView) view.findViewById(R.id.countTvVal);
  20. uni = (TextView) view.findViewById(R.id.countTvUni);
  21. myCheck = (CheckBox) view.findViewById(R.id.myCheck);
  22. }
  23. }
  24.  
  25.  
  26. @Override
  27. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  28. Log.d("getViewTest", "new view");
  29.  
  30. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  31. View view = inflater.inflate(layout, parent, false);
  32. ViewHolder viewHolder = new ViewHolder(view);
  33. view.setTag(viewHolder);
  34. return view;
  35. }
  36.  
  37.  
  38. @Override
  39. public void bindView(View view, Context context, Cursor cursor) {
  40.  
  41. Log.d("valuecheck","bindviewcalled");
  42.  
  43. ViewHolder holder = (ViewHolder) view.getTag();
  44.  
  45. String ingredients = cursor.getString(cursor.getColumnIndex(DB.COLUMN_INGR));
  46. String values = cursor.getString(cursor.getColumnIndex(DB.COLUMN_VAL));
  47. String units = cursor.getString(cursor.getColumnIndex(DB.COLUMN_UNI));
  48. String check = cursor.getString(cursor.getColumnIndex((DB.COLUMN_CHEKBOX)));
  49. int _ID = cursor.getInt(cursor.getColumnIndex(DB.COLUMN_ID));
  50.  
  51. holder.ingr.setText(ingredients);
  52. holder.val.setText(values);
  53. holder.uni.setText(units);
  54. holder.myCheck.setFocusable(false);
  55.  
  56. if (check.equals("1")){
  57. holder.myCheck.setChecked(true);
  58. }
  59. else {
  60. holder.myCheck.setChecked(false);
  61. }
  62.  
  63. holder.myCheck.setTag(new Model(_ID, ingredients, values, units, check));
  64.  
  65. }
  66.  
  67. }
  68.  
  69. public class Model {
  70.  
  71. long id;
  72. String mIngr;
  73. String mVal;
  74. String mUni;
  75. boolean mBox;
  76.  
  77.  
  78. public Model(long id, String ingr, String val, String uni, String box) {
  79.  
  80. this.id = id;
  81. this.mIngr = ingr;
  82. this.mVal = val;
  83. this.mUni = uni;
  84. this.mBox = (box.equals("1"))? true:false;
  85. }
  86.  
  87. }
  88.  
  89. public class CountingFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
  90.  
  91. private static final int CM_DELETE_ID = 1;
  92.  
  93. public static final int COUNT_LOADER = 2;
  94.  
  95. ListView lvData;
  96. DB db;
  97. CursorCountingAdapter scAdapter;
  98. CheckBox mCheckBox;
  99.  
  100.  
  101.  
  102. @Override
  103. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  104. Bundle savedInstanceState) {
  105.  
  106. View v = inflater.inflate(R.layout.fragment_counting, container, false);
  107.  
  108. lvData = (ListView)v.findViewById(R.id.lvDataCount);
  109. mCheckBox = (CheckBox)v.findViewById(R.id.myCheck);
  110.  
  111.  
  112.  
  113. lvData.setItemsCanFocus(false);
  114.  
  115. db = new DB(getActivity());
  116. db.open();
  117.  
  118.  
  119. // формируем столбцы сопоставления
  120. String[] from = new String[] {DB.COLUMN_INGR, DB.COLUMN_VAL, DB.COLUMN_UNI};
  121. final int[] to = new int[] {R.id.countTvIngr, R.id.countTvVal, R.id.countTvUni};
  122.  
  123.  
  124. scAdapter = new CursorCountingAdapter(getActivity(), null);
  125.  
  126. lvData.setAdapter(scAdapter);
  127. registerForContextMenu(lvData);
  128.  
  129. getActivity().getSupportLoaderManager().initLoader(COUNT_LOADER, null, this);
  130.  
  131. return v;
  132.  
  133. }
  134.  
  135. @Override
  136. public void onStart() {
  137. super.onStart();
  138. getActivity().getSupportLoaderManager().restartLoader(COUNT_LOADER, null, this);
  139.  
  140. }
  141.  
  142.  
  143. public void onCreateContextMenu(ContextMenu menu, View v,
  144. ContextMenu.ContextMenuInfo menuInfo) {
  145. super.onCreateContextMenu(menu, v, menuInfo);
  146. menu.add(0, CM_DELETE_ID, 0, R.string.delete_record);
  147. }
  148.  
  149. public boolean onContextItemSelected(MenuItem item) {
  150. if (item.getItemId() == CM_DELETE_ID) {
  151. // получаем из пункта контекстного меню данные по пункту списка
  152. AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item
  153. .getMenuInfo();
  154. // извлекаем id записи и удаляем соответствующую запись в БД
  155. db.delRec(acmi.id);
  156. // получаем новый курсор с данными
  157. getActivity().getSupportLoaderManager().getLoader(COUNT_LOADER).forceLoad();
  158. return true;
  159. }
  160. return super.onContextItemSelected(item);
  161. }
  162.  
  163.  
  164.  
  165. @Override
  166. public void onDestroyView() {
  167. super.onDestroyView();
  168. db.close();
  169. }
  170.  
  171.  
  172. @Override
  173. public Loader<Cursor> onCreateLoader(int id, Bundle args) {
  174.  
  175. switch (id){
  176. case COUNT_LOADER:
  177. String name = getArguments().getString("name"); //строка для получения аргументов создается в onCreateLoader
  178. return new CursorLoaderCount(getActivity(), db, id, name);
  179. }
  180.  
  181. return null;
  182. }
  183.  
  184.  
  185.  
  186. @Override
  187. public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  188. scAdapter.swapCursor(data);
  189. }
  190.  
  191. @Override
  192. public void onLoaderReset(Loader loader) {
  193. scAdapter.swapCursor(null);
  194.  
  195. }
  196.  
  197.  
  198. static class CursorLoaderCount extends CursorLoader {
  199. Cursor cursor;
  200. final int LoaderId;
  201. DB db;
  202. String name;
  203.  
  204. public CursorLoaderCount(Context context, DB db, int id, String name) {
  205. super(context);
  206. this.db = db;
  207. LoaderId = id;
  208. this.name = name;
  209. }
  210.  
  211. @Override
  212. public Cursor loadInBackground() {
  213.  
  214. switch (LoaderId){
  215.  
  216. case COUNT_LOADER:
  217. cursor = db.getIngrNew(name);
  218. break;
  219. }
  220.  
  221. return cursor;
  222. }
  223. }
  224.  
  225. }
  226.  
  227. public class DB {
  228.  
  229.  
  230. private static final String DB_NAME = "mealsDbb";
  231. private static final int DB_VERSION = 1;
  232. private static final String DB_TABLE = "myMeals";
  233.  
  234. public static final String COLUMN_ID = "_id";
  235. public static final String COLUMN_MEAL = "mealing";
  236. public static final String COLUMN_IMG = "img";
  237. public static final String COLUMN_INGR = "ingr";
  238. public static final String COLUMN_VAL = "val";
  239. public static final String COLUMN_UNI = "uni";
  240. public static final String COLUMN_IMGV3 = "img3";
  241. public static final String COLUMN_CHEKBOX = "myCheck";
  242.  
  243.  
  244.  
  245.  
  246. //private static String DB_DELETE = "drop table " + DB_TABLE + ");";
  247.  
  248.  
  249. private static final String DB_CREATE =
  250. "create table " + DB_TABLE + "(" +
  251. COLUMN_ID + " integer primary key autoincrement, " +
  252. COLUMN_IMG + " integer, " +
  253. COLUMN_MEAL + " text, " +
  254. COLUMN_IMGV3 + " integer, " +
  255. COLUMN_INGR + " text, " +
  256. COLUMN_VAL + " text, " +
  257. COLUMN_UNI + " text, " +
  258. COLUMN_CHEKBOX + " integer" +
  259. ");";
  260.  
  261. private final Context mCtx;
  262.  
  263.  
  264. private DBHelper mDBHelper;
  265. private SQLiteDatabase mDB;
  266.  
  267. public DB(Context ctx) {
  268. mCtx = ctx;
  269. }
  270.  
  271.  
  272. public void checkedTrue (int position){
  273. mDB.execSQL("UPDATE " + DB_TABLE + " SET " + COLUMN_CHEKBOX + " = '1' WHERE _id = " + position);
  274. }
  275.  
  276. public void checkedFalse (int position){
  277. mDB.execSQL("UPDATE " + DB_TABLE + " SET " + COLUMN_CHEKBOX + " = '0' WHERE _id = " + position);
  278. }
  279.  
  280.  
  281. // открыть подключение
  282. public void open() {
  283. mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
  284. mDB = mDBHelper.getWritableDatabase();
  285. }
  286.  
  287. // закрыть подключение
  288. public void close() {
  289. if (mDBHelper != null) mDBHelper.close();
  290. }
  291.  
  292. // получить все данные из таблицы DB_TABLE
  293. public Cursor getAllData() {
  294. 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);
  295. }
  296.  
  297.  
  298. public Cursor getMeal () {
  299. //return mDB.query(DB_TABLE, new String[]{COLUMN_ID, COLUMN_MEAL}, null, null, null, null, null);
  300.  
  301. return mDB.query(DB_TABLE, new String[]{COLUMN_ID, COLUMN_MEAL, COLUMN_IMGV3}, null, null, COLUMN_MEAL, null, null);
  302. }
  303.  
  304. public Cursor getIngr () {
  305. 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);
  306. }
  307.  
  308. public Cursor getIngrNew (String name) {
  309. 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);
  310. }
  311.  
  312.  
  313.  
  314.  
  315. // добавить запись в DB_TABLE
  316. public void addRec(int img, int img3, String mealing, String ingr, String val, String uni, int checkbox) {
  317. ContentValues cv = new ContentValues();
  318. cv.put(COLUMN_UNI, uni);
  319. cv.put(COLUMN_VAL, val);
  320. cv.put(COLUMN_INGR, ingr);
  321. cv.put(COLUMN_MEAL, mealing);
  322. cv.put(COLUMN_IMG, img);
  323. cv.put(COLUMN_IMGV3, img3);
  324. cv.put(COLUMN_CHEKBOX, checkbox);
  325.  
  326. mDB.insert(DB_TABLE, null, cv);
  327. }
  328.  
  329. // удалить запись из DB_TABLE
  330. public void delRec(long id) {
  331. mDB.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
  332. }
  333.  
  334.  
  335. // класс по созданию и управлению БД
  336. public class DBHelper extends SQLiteOpenHelper {
  337.  
  338. public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
  339. int version) {
  340. super(context, name, factory, version);
  341. }
  342.  
  343. // создаем и заполняем БД
  344. @Override
  345. public void onCreate(SQLiteDatabase db) {
  346. db.execSQL(DB_CREATE);
  347.  
  348.  
  349. }
  350.  
  351. @Override
  352. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  353.  
  354.  
  355. }
  356. }
  357.  
  358. }
  359.  
  360. public class CheckableRelativeLayout extends RelativeLayout implements
  361. Checkable {
  362.  
  363. private boolean isChecked;
  364. private List<Checkable> checkableViews;
  365.  
  366. public CheckableRelativeLayout(Context context, AttributeSet attrs,
  367. int defStyle) {
  368. super(context, attrs, defStyle);
  369. initialise(attrs);
  370. }
  371.  
  372. public CheckableRelativeLayout(Context context, AttributeSet attrs) {
  373. super(context, attrs);
  374. initialise(attrs);
  375. }
  376.  
  377. public CheckableRelativeLayout(Context context, int checkableId) {
  378. super(context);
  379. initialise(null);
  380. }
  381.  
  382. /*
  383. * @see android.widget.Checkable#isChecked()
  384. */
  385. public boolean isChecked() {
  386. return isChecked;
  387. }
  388.  
  389. /*
  390. * @see android.widget.Checkable#setChecked(boolean)
  391. */
  392. public void setChecked(boolean isChecked) {
  393. this.isChecked = isChecked;
  394. for (Checkable c : checkableViews) {
  395. c.setChecked(isChecked);
  396. }
  397. }
  398.  
  399. /*
  400. * @see android.widget.Checkable#toggle()
  401. */
  402. public void toggle() {
  403. this.isChecked = !this.isChecked;
  404. for (Checkable c : checkableViews) {
  405. c.toggle();
  406. }
  407. }
  408.  
  409. @Override
  410. protected void onFinishInflate() {
  411. super.onFinishInflate();
  412.  
  413. final int childCount = this.getChildCount();
  414. for (int i = 0; i < childCount; ++i) {
  415. findCheckableChildren(this.getChildAt(i));
  416. }
  417. }
  418.  
  419. /**
  420. * Read the custom XML attributes
  421. */
  422. private void initialise(AttributeSet attrs) {
  423. this.isChecked = false;
  424. this.checkableViews = new ArrayList<Checkable>(5);
  425. }
  426.  
  427. /**
  428. * Add to our checkable list all the children of the view that implement the
  429. * interface Checkable
  430. */
  431. private void findCheckableChildren(View v) {
  432. if (v instanceof Checkable) {
  433. this.checkableViews.add((Checkable) v);
  434. }
  435.  
  436. if (v instanceof ViewGroup) {
  437. final ViewGroup vg = (ViewGroup) v;
  438. final int childCount = vg.getChildCount();
  439. for (int i = 0; i < childCount; ++i) {
  440. findCheckableChildren(vg.getChildAt(i));
  441. }
  442. }
  443. }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement