Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private RecyclerView recyclerView;
  4. private itemAdapter itemAdapter;
  5. private RecyclerView.LayoutManager layoutManager;
  6. Button add_item_btn;
  7. public static final String myPreferences = "Prefs";
  8. public static final String TaskKey = "taskKey";
  9. SharedPreferences sharedPreferences;
  10.  
  11. ArrayList<data> myList = new ArrayList<>();
  12.  
  13. EditText editText;
  14. String task;
  15.  
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  22. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  23.  
  24. recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  25. itemAdapter = new itemAdapter(myList);
  26. final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  27. layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  28. recyclerView.setLayoutManager(layoutManager);
  29. recyclerView.setAdapter(itemAdapter);
  30.  
  31.  
  32. editText = (EditText) findViewById(R.id.edit_text);
  33.  
  34. SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
  35. try {
  36. myList = (ArrayList) ObjectSerializer.deserialize(sharedPreferences.getString("UserList", ObjectSerializer.serialize(new ArrayList())));
  37. itemAdapter.notifyData(myList);
  38. } catch (ClassNotFoundException e) {
  39. e.printStackTrace();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43.  
  44. // task=sharedPreferences.getString(getString(R.string.TEXT_INFO),"");
  45.  
  46.  
  47. //
  48. // data mLog=new data();
  49. // mLog.setTask(task);
  50. // myList.add(mLog);
  51.  
  52.  
  53. editText.setOnKeyListener(new View.OnKeyListener() {
  54. public boolean onKey(View v, int keyCode, KeyEvent event) {
  55. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  56. switch (keyCode) {
  57. case KeyEvent.KEYCODE_DPAD_CENTER:
  58. case KeyEvent.KEYCODE_ENTER:
  59.  
  60. data dataList = new data();
  61.  
  62. task = editText.getText().toString();
  63.  
  64. // editor.putString(getString(R.string.TEXT_INFO), editText.getText().toString());
  65. // editor.commit();
  66.  
  67.  
  68. if (task.matches("")) {
  69. Toast.makeText(v.getContext(), "Please enter a task", Toast.LENGTH_SHORT).show();
  70. }
  71.  
  72. data dataForRecycler = new data();
  73. dataForRecycler.setTask(task);
  74. myList.add(dataForRecycler);
  75.  
  76. SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(getString(R.string.PREF_FILE), MODE_PRIVATE);
  77. SharedPreferences.Editor editor = sharedPreferences.edit();
  78.  
  79. try {
  80. editor.putString(getString(R.string.TEXT_INFO), ObjectSerializer.serialize(myList));
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. editor.commit();
  85.  
  86.  
  87. itemAdapter.notifyData(myList);
  88. editText.setText(null);
  89. editText.setVisibility(View.GONE);
  90. // InputMethodManager inputManager = (InputMethodManager)
  91. // getSystemService(Context.INPUT_METHOD_SERVICE);
  92. // inputManager.hideSoftInputFromWindow((null == getCurrentFocus()) ? null : getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
  93. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  94. imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
  95.  
  96.  
  97. return true;
  98. default:
  99. break;
  100. }
  101. }
  102. return false;
  103. }
  104. });
  105.  
  106.  
  107. add_item_btn = (Button) findViewById(R.id.add_item_button);
  108. add_item_btn.setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View v) {
  111.  
  112. editText.setVisibility(View.VISIBLE);
  113. editText.requestFocus();
  114. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  115. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
  116.  
  117. }
  118. });
  119. }
  120.  
  121.  
  122. }
  123.  
  124. public class data implements Serializable {
  125. private String task;
  126. private boolean isChecked;
  127. RecyclerView info;
  128.  
  129. //Default Constructor
  130. public data()
  131. {}
  132.  
  133. //overloaded constructor
  134.  
  135. public data (String task, boolean isChecked)
  136. {
  137. this.task=task;
  138. this.isChecked=isChecked;
  139. }
  140.  
  141. //getter to return value of String variable task
  142. public String getTask()
  143. {return task;}
  144.  
  145. //getter to return value of boolean isChecked
  146.  
  147. public boolean isChecked()
  148. {return isChecked;}
  149.  
  150. //setter for string variable task
  151. public void setTask(String task)
  152. {
  153. this.task=task;
  154. }
  155.  
  156. //setter for boolean isChecked
  157. public void setChecked(boolean isChecked)
  158. {
  159. this.isChecked=isChecked;
  160. }
  161. }
  162.  
  163. public class itemAdapter extends RecyclerView.Adapter<itemAdapter.RecyclerItemViewHolder> {
  164. private ArrayList<data> myList;
  165. int mLastPosition = 0;
  166.  
  167. public itemAdapter(ArrayList<data> myList) {
  168. this.myList = myList;
  169.  
  170. }
  171. public RecyclerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  172. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
  173. RecyclerItemViewHolder holder = new RecyclerItemViewHolder(view);
  174. return holder;
  175. }
  176. @Override
  177. public void onBindViewHolder(RecyclerItemViewHolder holder, final int position) {
  178. Log.d("onBindViewHoler ", myList.size() + "");
  179. holder.etTaskTextView.setText(myList.get(position).getTask());
  180. mLastPosition =position;
  181. }
  182. @Override
  183. public int getItemCount() {
  184. return(null != myList?myList.size():0);
  185. }
  186. public void notifyData(ArrayList<data> myList) {
  187. Log.d("notifyData ", myList.size() + "");
  188. this.myList = myList;
  189. notifyDataSetChanged();
  190. }
  191. public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {
  192. private final TextView etTaskTextView;
  193.  
  194. private LinearLayout mainLayout;
  195. public RecyclerItemViewHolder(final View parent) {
  196. super(parent);
  197. etTaskTextView = (TextView) parent.findViewById(R.id.task_text);
  198.  
  199. mainLayout = (LinearLayout) parent.findViewById(R.id.list_linear_layout);
  200. mainLayout.setOnClickListener(new View.OnClickListener() {
  201. @Override
  202. public void onClick(View v) {
  203. Toast.makeText(itemView.getContext(), "Position:" + Integer.toString(getPosition()), Toast.LENGTH_SHORT).show();
  204. }
  205. });
  206.  
  207. }
  208. }
  209. }
  210.  
  211. public class ObjectSerializer {
  212.  
  213. public static String serialize(Serializable obj) throws IOException {
  214. if (obj == null) return "";
  215. ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
  216. ObjectOutputStream objStream = new ObjectOutputStream(serialObj);
  217. objStream.writeObject(obj);
  218. objStream.close();
  219. return encodeBytes(serialObj.toByteArray());
  220. }
  221.  
  222. public static Object deserialize(String str) throws IOException, ClassNotFoundException {
  223. if (str == null || str.length() == 0) return null;
  224. ByteArrayInputStream serialObj = new ByteArrayInputStream(decodeBytes(str));
  225. ObjectInputStream objStream = new ObjectInputStream(serialObj);
  226. return objStream.readObject();
  227. }
  228.  
  229. public static String encodeBytes(byte[] bytes) {
  230. StringBuffer strBuf = new StringBuffer();
  231.  
  232. for (int i = 0; i < bytes.length; i++) {
  233. strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
  234. strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
  235. }
  236.  
  237. return strBuf.toString();
  238. }
  239.  
  240. public static byte[] decodeBytes(String str) {
  241. byte[] bytes = new byte[str.length() / 2];
  242. for (int i = 0; i < str.length(); i+=2) {
  243. char c = str.charAt(i);
  244. bytes[i/2] = (byte) ((c - 'a') << 4);
  245. c = str.charAt(i+1);
  246. bytes[i/2] += (c - 'a');
  247. }
  248. return bytes;
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement