Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. public class Itemadapter extends RecyclerView.Adapter<Itemadapter.itemviewholder> {
  2. static ArrayList<Item> items = new ArrayList<Item>();
  3. Context mcontext;
  4.  
  5. private String mFilename = utils.EXTRAS_NOTE_FILENAME;
  6.  
  7.  
  8. public Itemadapter(MainActivity mainActivity, int item, ArrayList<Item> items)
  9. {
  10. this.items = items;
  11. }
  12.  
  13.  
  14. @Override
  15. public itemviewholder onCreateViewHolder(ViewGroup parent, int viewType) {
  16. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
  17. itemviewholder itemviewholder = new itemviewholder(view);
  18. return itemviewholder;
  19.  
  20.  
  21.  
  22.  
  23.  
  24. }
  25.  
  26. @Override
  27. public void onBindViewHolder(final itemviewholder holder, final int position) {
  28. Item item = items.get(position);
  29. holder.postnumber.setText(item.getPost_number());
  30. holder.itemname.setText(item.getItem_name());
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. }
  39.  
  40.  
  41.  
  42. @Override
  43. public int getItemCount() {
  44. return items.size();
  45.  
  46. }
  47. public class itemviewholder extends RecyclerView.ViewHolder implements View.OnClickListener
  48.  
  49. {
  50.  
  51.  
  52.  
  53. TextView itemname,postnumber,ico;
  54. private PopupMenu popupMenu;
  55. ImageButton ico1;
  56.  
  57. public itemviewholder(View view)
  58. {
  59. super(view);
  60.  
  61.  
  62. itemname = view.findViewById(R.id.itemdetailsdisplay);
  63. postnumber = view.findViewById(R.id.itemnuberdisplay);
  64. ico1 = view.findViewById(R.id.test);
  65. view.setOnClickListener(this);
  66. ico1.setOnClickListener(this);
  67.  
  68.  
  69.  
  70. }
  71.  
  72.  
  73. @Override
  74. public void onClick(final View v) {
  75. final Item mLoadeditem = utils.getitembyfilename(v.getContext(),mFilename);
  76.  
  77. if (v.getId() == ico1.getId()){
  78. // Toast.makeText(v.getContext(), "ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
  79. popupMenu = new PopupMenu(v.getContext(),v);
  80. popupMenu.inflate(R.menu.recycler_item_menu);
  81. popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
  82. @Override
  83. public boolean onMenuItemClick(MenuItem item) {
  84. switch (item.getItemId()){
  85. case R.id.item_delete:
  86. //delete file
  87. utils.delete(v.getContext(),mLoadeditem.getPost_number()+utils.FILE_EXTENSION);
  88. items.remove(getAdapterPosition());
  89. notifyDataSetChanged();
  90. Toast.makeText(v.getContext(),"item deleted",Toast.LENGTH_SHORT).show();
  91.  
  92. break;
  93. case R.id.item_share:
  94. //share info
  95. Toast.makeText(v.getContext(),"item shared",Toast.LENGTH_SHORT).show();
  96.  
  97. break;
  98. }
  99.  
  100. return false;
  101. }
  102. });popupMenu.show();
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. }
  110. else {
  111. Toast.makeText(v.getContext(), "ROW PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
  112. }
  113.  
  114.  
  115. }
  116. }
  117. }
  118.  
  119. public class utils {
  120.  
  121. public static final String FILE_EXTENSION = ".bin";
  122. public static final String EXTRAS_NOTE_FILENAME = "EXRAS_NOTES_FILENAME";
  123.  
  124. public static boolean save(Context context, Item item){
  125. String filename = String.valueOf(item.getPost_number()) +FILE_EXTENSION;
  126.  
  127. FileOutputStream fos;
  128. ObjectOutputStream oos;
  129. try {
  130. fos= context.openFileOutput(filename,context.MODE_PRIVATE);
  131. oos= new ObjectOutputStream(fos);
  132. oos.writeObject(item);
  133. oos.close();
  134. fos.close();
  135.  
  136. }catch (Exception e){
  137. e.printStackTrace();
  138. return false;
  139. //something went wrong
  140. }
  141.  
  142.  
  143. return true;
  144.  
  145. }
  146. public static ArrayList<Item> getallsaveditems(Context context){
  147. ArrayList <Item> items= new ArrayList<>();
  148. File filesdir = context.getFilesDir();
  149. ArrayList<String> itemsfiles = new ArrayList<>();
  150. for(String file : filesdir.list()){
  151. if(file.endsWith(FILE_EXTENSION)){
  152. itemsfiles.add(file);
  153. }
  154. }
  155.  
  156. FileInputStream fis;
  157. ObjectInputStream ois;
  158.  
  159. for(int i=0; i<itemsfiles.size();i++){
  160. try {
  161. fis = context.openFileInput(itemsfiles.get(i));
  162. ois = new ObjectInputStream(fis);
  163. items.add((Item) ois.readObject());
  164. fis.close();
  165. ois.close();
  166. }catch (IOException | ClassNotFoundException e){
  167. e.printStackTrace();
  168. return null;
  169. }
  170. }
  171. return items;
  172. }
  173.  
  174. public static Item getitembyfilename(Context context,String filename) {
  175. File file = new File(context.getFilesDir(), filename);
  176. if (file.exists() && !file.isDirectory()) {
  177. Log.v("utils", "file exists" + filename);
  178. FileInputStream fis;
  179. ObjectInputStream ois;
  180. try {
  181. fis = context.openFileInput(filename);
  182. ois = new ObjectInputStream(fis);
  183. Item item = (Item) ois.readObject();
  184. fis.close();
  185. ois.close();
  186. return item;
  187. } catch (IOException |ClassNotFoundException e) {
  188. e.printStackTrace();
  189. return null;
  190. }
  191. }else {
  192. return null;
  193.  
  194.  
  195.  
  196.  
  197. }
  198.  
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. public static void delete(Context ctx ,String filename) {
  210. File dir = ctx.getFilesDir();
  211. File file = new File(dir,filename);
  212. if(file.exists()){
  213. file.delete();
  214. }
  215.  
  216.  
  217. }
  218.  
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement