Advertisement
Guest User

java adapter

a guest
Feb 9th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.76 KB | None | 0 0
  1. public class InboxAdapter extends ArrayAdapter<InboxBO> {
  2.  
  3.     NotificationInbox fragment;
  4.     Context context;
  5.     List<InboxBO> inboxForSharedPref;
  6.     List<InboxBO> inboxList;
  7.     int layoutResID;
  8.     SharedPreferences prefs;
  9.     private ArrayList<InboxBO> inboxarraylist;
  10.     private SparseBooleanArray mSelectedItemIds;
  11.  
  12.     public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
  13.         super(context, layoutResourceID, objs);
  14.         mSelectedItemIds = new SparseBooleanArray();
  15.         this.context = context;
  16.         this.inboxList = objs;
  17.         this.layoutResID = layoutResourceID;
  18.         this.inboxarraylist = new ArrayList<InboxBO>();
  19.         this.inboxarraylist.addAll(inboxList);
  20.         this.fragment = fragment;
  21.     }
  22.  
  23.  
  24.  
  25.     @Override
  26.     public View getView(int position, View convertView, ViewGroup parent) {
  27.         inboxHolder inboxholder;
  28.         View view = convertView;
  29.         if (view == null) {
  30.             view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
  31.             //LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
  32.             //LayoutInflater inflater = LayoutInflater.from(getContext());
  33.             //inflater.inflate(R.layout.activity_inbox_item, null);
  34.             inboxholder = new inboxHolder();
  35.             //view = inflater.inflate(layoutResID, parent, false);
  36.             prefs = context.getSharedPreferences(
  37.                     Constants.PREF_NAME, 0);
  38.  
  39.             inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
  40.             inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
  41.             inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
  42.             inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
  43.             inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
  44.             inboxholder.delete = (TextView)view.findViewById(R.id.delete);
  45.             inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
  46.             inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
  47.             inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
  48.             view.setTag(inboxholder);
  49.         }
  50.         else {
  51.             inboxholder = (inboxHolder) view.getTag();
  52.         }
  53.  
  54.         InboxBO mItem =     inboxList.get(position);
  55.         if(mItem!=null){
  56.             inboxholder.senderNameText.setText((String)mItem.getTitle());
  57.             inboxholder.pushDateText.setText(new Date().toString());
  58.             inboxholder.pushTimeText.setText(new Date().toString());
  59.             inboxholder.messageText.setText((String)mItem.getMessage());
  60.         }
  61.         return view;
  62.     }
  63.  
  64.     // For swipe action
  65.     private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
  66.         return new View.OnClickListener() {
  67.             @Override
  68.             public void onClick(View v) {
  69.                 AlertDialog.Builder alert = new AlertDialog.Builder(
  70.                         context);
  71.                 alert.setTitle("Delete Message");
  72.                 alert.setMessage("Are you sure you wish to delete this message?");
  73.                 alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  74.  
  75.                     @Override
  76.                     public void onClick(DialogInterface dialog, int which) {
  77.                         inboxList.remove(position);
  78.                         Gson gson = new Gson();
  79.                         inboxForSharedPref = fragment.getStoredMessages();
  80.                         inboxForSharedPref = inboxList;
  81.                         String jsonSavePref = gson.toJson(inboxForSharedPref);
  82.                         fragment.commitToStoredList(jsonSavePref);
  83.                         Toast.makeText(context, "Message Deleted!",
  84.                                 Toast.LENGTH_SHORT).show();
  85.                         holder.swipeLayout.close();
  86.                         fragment.retrieveMessage();
  87.                         dialog.dismiss();
  88.                     }
  89.                 });
  90.  
  91.                 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  92.  
  93.                     @Override
  94.                     public void onClick(DialogInterface dialog, int which) {
  95.  
  96.                         dialog.dismiss();
  97.                     }
  98.                 });
  99.  
  100.                 alert.show();
  101.             }
  102.         };
  103.     }
  104.  
  105.     // Algorithm to filter out listview based on text changed in listview searchbox
  106.     public void filter(String charText) {
  107.         charText = charText.toLowerCase(Locale.getDefault());
  108.         inboxList.clear();
  109.         if (charText.length() == 0) {
  110.             inboxList.addAll(inboxarraylist);
  111.         }
  112.         else
  113.         {
  114.             for (InboxBO ilist : inboxarraylist)
  115.             {
  116.                 if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
  117.                 {
  118.                     inboxList.add(ilist);
  119.                 }
  120.             }
  121.         }
  122.         notifyDataSetChanged();
  123.     }
  124.  
  125.  
  126.     public static class inboxHolder {
  127.         TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
  128.         RelativeLayout itemLayout;
  129.         private SwipeLayout swipeLayout;
  130.     }
  131.  
  132.     // Methods below are for multi-deletion
  133.     public void  toggleSelection(int position) {
  134.         selectView(position, !mSelectedItemIds.get(position));
  135.     }
  136.     // Remove selection after unchecked
  137.     public void  remove(InboxBO object) {
  138.         inboxList.remove(object);
  139.         Gson gson = new Gson();
  140.         inboxForSharedPref = fragment.getStoredMessages();
  141.         inboxForSharedPref = inboxList;
  142.         String jsonSavePref = gson.toJson(inboxForSharedPref);
  143.         fragment.commitToStoredList(jsonSavePref);
  144.         notifyDataSetChanged();
  145.     }
  146.  
  147.     // Item checked on selection
  148.     public void selectView(int position, boolean value) {
  149.         if (value)
  150.             mSelectedItemIds.put(position,  value);
  151.         else
  152.             mSelectedItemIds.delete(position);
  153.         notifyDataSetChanged();
  154.     }
  155.  
  156.     // Get number of selected item
  157.     public int  getSelectedCount() {
  158.         return mSelectedItemIds.size();
  159.     }
  160.  
  161.     public  SparseBooleanArray getSelectedIds() {
  162.         return mSelectedItemIds;
  163.     }
  164.  
  165.     public void removeSelection() {
  166.         mSelectedItemIds = new SparseBooleanArray();
  167.         notifyDataSetChanged();
  168.     }
  169.  
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement