Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.40 KB | None | 0 0
  1. public class CustomListAdapterInterests extends ArrayAdapter < String > {
  2.  
  3. private final Activity context;
  4. private final ArrayList < String > mItemInterest;
  5.  
  6. public CustomListAdapterInterests(Activity context, ArrayList < String > itemInterest) {
  7. super(context, R.layout.list_item_interests, itemInterest);
  8.  
  9. this.context = context;
  10. this.mItemInterest = itemInterest;
  11. }
  12.  
  13. @Override
  14. public int getCount() {
  15. return mItemInterest.size();
  16. }
  17.  
  18. public View getView(int position, View view, ViewGroup parent) {
  19.  
  20. LayoutInflater inflater = context.getLayoutInflater();
  21. View rowView = inflater.inflate(R.layout.list_item_interests, null, true);
  22.  
  23. TextView itemInterestTV = (TextView) rowView.findViewById(R.id.textInterest);
  24.  
  25. itemInterestTV.setText(mItemInterest.get(position));
  26. return rowView;
  27. }
  28. }
  29.  
  30. public class InterestsFragment extends BaseFragment {
  31.  
  32. private ArrayList < String > mInterestList;
  33. private static final int MAX_STORED_LINES_INTERESTS = 50;
  34. private FloatingActionButton plusInterestsBTN;
  35. private CustomListAdapterInterests adapterInterests;
  36. private ListView listInterests;
  37. private EditText interestET;
  38. private Button confirmInterestBTN;
  39. public SharedPreferences sharedPreferences;
  40.  
  41. @Override
  42. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  43. View v = inflater.inflate(R.layout.fragment_interests, container, false);
  44.  
  45. plusInterestsBTN = (FloatingActionButton) v.findViewById(R.id.plusInterests);
  46. sharedPreferences = getActivity().getSharedPreferences(Constants.PREFERENCES_INTERESTS, Context.MODE_PRIVATE);
  47. mInterestList = new ArrayList < String > ();
  48. loadInterestFromPreferences(mInterestList);
  49. adapterInterests = new CustomListAdapterInterests(getActivity(), mInterestList);
  50.  
  51. listInterests = (ListView) v.findViewById(R.id.listViewInterests);
  52. listInterests.setAdapter(adapterInterests);
  53.  
  54. listInterests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  55. public void onItemClick(AdapterView <? > arg0, View v, int position, long arg3) {
  56.  
  57.  
  58. if (sharedPreferences.contains(Constants.INTEREST + position)) {
  59. SharedPreferences.Editor editor = sharedPreferences.edit();
  60. mInterestList.remove(position);
  61. adapterInterests.notifyDataSetChanged();
  62. editor.remove(Constants.INTEREST + position);
  63.  
  64.  
  65.  
  66. editor.commit();
  67. }
  68. }
  69. });
  70.  
  71. listInterests.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Override
  72. public boolean onItemLongClick(AdapterView <? > arg0, View arg1,
  73. final int position, long id) {
  74. onShowDialogSetItem(position);
  75. return true;
  76. }
  77. });
  78.  
  79. plusInterestsBTN.setOnClickListener(new View.OnClickListener() {@Override
  80. public void onClick(View v) {
  81. onShowDialogAddItem();
  82. }
  83.  
  84. });
  85.  
  86. listInterests.setOnScrollListener(new AbsListView.OnScrollListener() {@Override
  87. public void onScrollStateChanged(AbsListView view, int scrollState) {
  88.  
  89. int btn_initPosY = plusInterestsBTN.getScrollY();
  90.  
  91. if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
  92. plusInterestsBTN.animate().cancel();
  93. plusInterestsBTN.animate().translationXBy(350);
  94. } else {
  95. plusInterestsBTN.animate().cancel();
  96. plusInterestsBTN.animate().translationX(btn_initPosY);
  97. }
  98. }
  99.  
  100. @Override
  101. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  102.  
  103. }
  104. });
  105. return v;
  106. }
  107.  
  108. private void loadInterestFromPreferences(ArrayList < String > mInterestList) {
  109. for (int x = 0; x < 5; x++) {
  110.  
  111. String interests = sharedPreferences.getString(Constants.INTEREST + x, Constants.DEFAULT);
  112.  
  113. Toast.makeText(getActivity(), interests, Toast.LENGTH_SHORT).show();
  114.  
  115. if (interests != "") {
  116. mInterestList.add(interests);
  117. }
  118.  
  119.  
  120. }
  121. }
  122.  
  123.  
  124. private void onShowDialogSetItem(final int position) {
  125. final Dialog dialogInterest = new Dialog(getActivity());
  126. dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
  127. dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
  128. dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
  129.  
  130. LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  131. View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
  132.  
  133. dialogInterest.setCanceledOnTouchOutside(true);
  134. dialogInterest.setContentView(view);
  135.  
  136. final EditText interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
  137. Button confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
  138. TextView title = (TextView) dialogInterest.findViewById(R.id.textView2);
  139. title.setText("Edit Interest");
  140. interestET.setText(mInterestList.get(position));
  141.  
  142. confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
  143.  
  144. @Override
  145. public void onClick(View v) {
  146. Log.d("2", "" + position);
  147. String interest = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
  148.  
  149.  
  150.  
  151. SharedPreferences.Editor editor = sharedPreferences.edit();
  152. editor.putString(Constants.INTEREST + position, interestET.getText().toString());
  153. editor.commit();
  154. String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
  155.  
  156. mInterestList.set(position, interestET.getText().toString());
  157.  
  158.  
  159. Toast.makeText(getActivity(), "Upravené: " + interests, Toast.LENGTH_SHORT).show();
  160. adapterInterests.notifyDataSetChanged();
  161. dialogInterest.dismiss();
  162.  
  163. }
  164. });
  165. dialogInterest.show();
  166. }
  167. private void onShowDialogAddItem() {
  168.  
  169. if (mInterestList.size() >= MAX_STORED_LINES_INTERESTS) {
  170. return;
  171. }
  172. final Dialog dialogInterest = new Dialog(getActivity());
  173. dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
  174. dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
  175. dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
  176.  
  177. LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  178. View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
  179.  
  180. dialogInterest.setCanceledOnTouchOutside(true);
  181. dialogInterest.setContentView(view);
  182.  
  183. interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
  184. confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
  185.  
  186. confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
  187.  
  188. @Override
  189. public void onClick(View v) {
  190. int position = listInterests.getAdapter().getCount();
  191. SharedPreferences.Editor editor = sharedPreferences.edit();
  192. editor.putString(Constants.INTEREST + position, interestET.getText().toString());
  193. editor.commit();
  194. String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
  195. Toast.makeText(getActivity(), "Přidané: " + interests, Toast.LENGTH_SHORT).show();
  196. mInterestList.add(interestET.getText().toString());
  197. //adapterInterests.notifyDataSetChanged();
  198.  
  199. dialogInterest.dismiss();
  200. }
  201. });
  202. dialogInterest.show();
  203. adapterInterests.notifyDataSetChanged();
  204. }
  205. }
  206.  
  207. //for save
  208. StringBuilder sb = new StringBuilder();
  209. for (String interest : mInterestList) {
  210. sb.append(interest).append(",");
  211. }
  212. prefsEditor.putString("MyInterests", sb.toString());
  213. prefsEditor.commit();
  214.  
  215. //for read
  216. String [] interests= sharedPreferences.getString("MyInterests");
  217. mInterestList = new ArrayList<String>(Arrays.asList(interests));
  218.  
  219. if (sharedPreferences.getString(Constants.INTEREST + position)!=null) {
  220. SharedPreferences.Editor editor = sharedPreferences.edit();
  221. mInterestList.remove(position);
  222. adapterInterests.notifyDataSetChanged();
  223. editor.remove(Constants.INTEREST + position);
  224.  
  225.  
  226.  
  227. editor.commit();
  228. }
  229.  
  230. @Override
  231. public void onClick(View view) {
  232.  
  233. boolean isChecked = mainHolder.chekenitem.isChecked();
  234.  
  235. // arr=getResources().getStringArray( mainHolder.txtenimgid.getText().toString());
  236. // boolean isChecked = mainHolder.chekenitem.isChecked();
  237. int i;
  238. String itemId1 = mainHolder.txtenimgid.getText().toString();
  239. SharedPreferences prefs=view.getContext().getSharedPreferences(MY_PREFS_NAME,Context.MODE_PRIVATE);
  240. SharedPreferences.Editor edit=prefs.edit();
  241. Set<String> set = new HashSet<String>();
  242. try {
  243. if (isChecked) {
  244.  
  245.  
  246. addmembers.add(itemId1);
  247.  
  248. for (j = 0; j < addmembers.size(); j++) {
  249. set.addAll(addmembers);
  250. edit.putStringSet("yourKey", set);
  251. edit.commit();
  252. Toast.makeText(view.getContext(), "Clicked on Checkbox addmembers[pos] : " + addmembers.get(j) + " Item Id is " + itemId1
  253. , Toast.LENGTH_LONG).show();
  254.  
  255. }
  256.  
  257. // editor.putString("key_name",itemId );
  258. // editor.apply();
  259.  
  260. // }
  261.  
  262. } else {
  263. for(int k=0;k<=addmembers.size();k++){
  264. if(addmembers.get(k).equals(itemId1)){
  265. // set.remove(addmembers);
  266. addmembers.remove(k);
  267. edit.remove(addmembers.get(k));
  268. edit.commit();
  269.  
  270. break;
  271. }
  272.  
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement