Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomListAdapterInterests extends ArrayAdapter < String > {
- private final Activity context;
- private final ArrayList < String > mItemInterest;
- public CustomListAdapterInterests(Activity context, ArrayList < String > itemInterest) {
- super(context, R.layout.list_item_interests, itemInterest);
- this.context = context;
- this.mItemInterest = itemInterest;
- }
- @Override
- public int getCount() {
- return mItemInterest.size();
- }
- public View getView(int position, View view, ViewGroup parent) {
- LayoutInflater inflater = context.getLayoutInflater();
- View rowView = inflater.inflate(R.layout.list_item_interests, null, true);
- TextView itemInterestTV = (TextView) rowView.findViewById(R.id.textInterest);
- itemInterestTV.setText(mItemInterest.get(position));
- return rowView;
- }
- }
- public class InterestsFragment extends BaseFragment {
- private ArrayList < String > mInterestList;
- private static final int MAX_STORED_LINES_INTERESTS = 50;
- private FloatingActionButton plusInterestsBTN;
- private CustomListAdapterInterests adapterInterests;
- private ListView listInterests;
- private EditText interestET;
- private Button confirmInterestBTN;
- public SharedPreferences sharedPreferences;
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.fragment_interests, container, false);
- plusInterestsBTN = (FloatingActionButton) v.findViewById(R.id.plusInterests);
- sharedPreferences = getActivity().getSharedPreferences(Constants.PREFERENCES_INTERESTS, Context.MODE_PRIVATE);
- mInterestList = new ArrayList < String > ();
- loadInterestFromPreferences(mInterestList);
- adapterInterests = new CustomListAdapterInterests(getActivity(), mInterestList);
- listInterests = (ListView) v.findViewById(R.id.listViewInterests);
- listInterests.setAdapter(adapterInterests);
- listInterests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- public void onItemClick(AdapterView <? > arg0, View v, int position, long arg3) {
- if (sharedPreferences.contains(Constants.INTEREST + position)) {
- SharedPreferences.Editor editor = sharedPreferences.edit();
- mInterestList.remove(position);
- adapterInterests.notifyDataSetChanged();
- editor.remove(Constants.INTEREST + position);
- editor.commit();
- }
- }
- });
- listInterests.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Override
- public boolean onItemLongClick(AdapterView <? > arg0, View arg1,
- final int position, long id) {
- onShowDialogSetItem(position);
- return true;
- }
- });
- plusInterestsBTN.setOnClickListener(new View.OnClickListener() {@Override
- public void onClick(View v) {
- onShowDialogAddItem();
- }
- });
- listInterests.setOnScrollListener(new AbsListView.OnScrollListener() {@Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- int btn_initPosY = plusInterestsBTN.getScrollY();
- if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
- plusInterestsBTN.animate().cancel();
- plusInterestsBTN.animate().translationXBy(350);
- } else {
- plusInterestsBTN.animate().cancel();
- plusInterestsBTN.animate().translationX(btn_initPosY);
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- }
- });
- return v;
- }
- private void loadInterestFromPreferences(ArrayList < String > mInterestList) {
- for (int x = 0; x < 5; x++) {
- String interests = sharedPreferences.getString(Constants.INTEREST + x, Constants.DEFAULT);
- Toast.makeText(getActivity(), interests, Toast.LENGTH_SHORT).show();
- if (interests != "") {
- mInterestList.add(interests);
- }
- }
- }
- private void onShowDialogSetItem(final int position) {
- final Dialog dialogInterest = new Dialog(getActivity());
- dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
- dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
- dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
- LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
- dialogInterest.setCanceledOnTouchOutside(true);
- dialogInterest.setContentView(view);
- final EditText interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
- Button confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
- TextView title = (TextView) dialogInterest.findViewById(R.id.textView2);
- title.setText("Edit Interest");
- interestET.setText(mInterestList.get(position));
- confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.d("2", "" + position);
- String interest = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Constants.INTEREST + position, interestET.getText().toString());
- editor.commit();
- String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
- mInterestList.set(position, interestET.getText().toString());
- Toast.makeText(getActivity(), "Upravené: " + interests, Toast.LENGTH_SHORT).show();
- adapterInterests.notifyDataSetChanged();
- dialogInterest.dismiss();
- }
- });
- dialogInterest.show();
- }
- private void onShowDialogAddItem() {
- if (mInterestList.size() >= MAX_STORED_LINES_INTERESTS) {
- return;
- }
- final Dialog dialogInterest = new Dialog(getActivity());
- dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top;
- dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE);
- dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName;
- LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false);
- dialogInterest.setCanceledOnTouchOutside(true);
- dialogInterest.setContentView(view);
- interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest);
- confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest);
- confirmInterestBTN.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- int position = listInterests.getAdapter().getCount();
- SharedPreferences.Editor editor = sharedPreferences.edit();
- editor.putString(Constants.INTEREST + position, interestET.getText().toString());
- editor.commit();
- String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT);
- Toast.makeText(getActivity(), "Přidané: " + interests, Toast.LENGTH_SHORT).show();
- mInterestList.add(interestET.getText().toString());
- //adapterInterests.notifyDataSetChanged();
- dialogInterest.dismiss();
- }
- });
- dialogInterest.show();
- adapterInterests.notifyDataSetChanged();
- }
- }
- //for save
- StringBuilder sb = new StringBuilder();
- for (String interest : mInterestList) {
- sb.append(interest).append(",");
- }
- prefsEditor.putString("MyInterests", sb.toString());
- prefsEditor.commit();
- //for read
- String [] interests= sharedPreferences.getString("MyInterests");
- mInterestList = new ArrayList<String>(Arrays.asList(interests));
- if (sharedPreferences.getString(Constants.INTEREST + position)!=null) {
- SharedPreferences.Editor editor = sharedPreferences.edit();
- mInterestList.remove(position);
- adapterInterests.notifyDataSetChanged();
- editor.remove(Constants.INTEREST + position);
- editor.commit();
- }
- @Override
- public void onClick(View view) {
- boolean isChecked = mainHolder.chekenitem.isChecked();
- // arr=getResources().getStringArray( mainHolder.txtenimgid.getText().toString());
- // boolean isChecked = mainHolder.chekenitem.isChecked();
- int i;
- String itemId1 = mainHolder.txtenimgid.getText().toString();
- SharedPreferences prefs=view.getContext().getSharedPreferences(MY_PREFS_NAME,Context.MODE_PRIVATE);
- SharedPreferences.Editor edit=prefs.edit();
- Set<String> set = new HashSet<String>();
- try {
- if (isChecked) {
- addmembers.add(itemId1);
- for (j = 0; j < addmembers.size(); j++) {
- set.addAll(addmembers);
- edit.putStringSet("yourKey", set);
- edit.commit();
- Toast.makeText(view.getContext(), "Clicked on Checkbox addmembers[pos] : " + addmembers.get(j) + " Item Id is " + itemId1
- , Toast.LENGTH_LONG).show();
- }
- // editor.putString("key_name",itemId );
- // editor.apply();
- // }
- } else {
- for(int k=0;k<=addmembers.size();k++){
- if(addmembers.get(k).equals(itemId1)){
- // set.remove(addmembers);
- addmembers.remove(k);
- edit.remove(addmembers.get(k));
- edit.commit();
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement