Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InboxAdapter extends ArrayAdapter<InboxBO> {
- NotificationInbox fragment;
- Context context;
- List<InboxBO> inboxForSharedPref;
- List<InboxBO> inboxList;
- int layoutResID;
- SharedPreferences prefs;
- private ArrayList<InboxBO> inboxarraylist;
- private SparseBooleanArray mSelectedItemIds;
- public InboxAdapter(Context context, int layoutResourceID, List<InboxBO> objs, NotificationInbox fragment) {
- super(context, layoutResourceID, objs);
- mSelectedItemIds = new SparseBooleanArray();
- this.context = context;
- this.inboxList = objs;
- this.layoutResID = layoutResourceID;
- this.inboxarraylist = new ArrayList<InboxBO>();
- this.inboxarraylist.addAll(inboxList);
- this.fragment = fragment;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- inboxHolder inboxholder;
- View view = convertView;
- if (view == null) {
- view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutResID, parent, false);
- //LayoutInflater inflater = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
- //LayoutInflater inflater = LayoutInflater.from(getContext());
- //inflater.inflate(R.layout.activity_inbox_item, null);
- inboxholder = new inboxHolder();
- //view = inflater.inflate(layoutResID, parent, false);
- prefs = context.getSharedPreferences(
- Constants.PREF_NAME, 0);
- inboxholder.swipeLayout = (SwipeLayout)view.findViewById(R.id.swipe_layout);
- inboxholder.senderNameText = (TextView) view.findViewById(R.id.senderNameText);
- inboxholder.pushDateText = (TextView) view.findViewById(R.id.pushDateText);
- inboxholder.pushTimeText = (TextView) view.findViewById(R.id.pushTimeText);
- inboxholder.messageText = (TextView) view.findViewById(R.id.messageText);
- inboxholder.delete = (TextView)view.findViewById(R.id.delete);
- inboxholder.itemLayout = (RelativeLayout) view.findViewById(R.id.relativeViewInbox);
- inboxholder.swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
- inboxholder.delete.setOnClickListener(onDeleteListener(position, inboxholder));
- view.setTag(inboxholder);
- }
- else {
- inboxholder = (inboxHolder) view.getTag();
- }
- InboxBO mItem = inboxList.get(position);
- if(mItem!=null){
- inboxholder.senderNameText.setText((String)mItem.getTitle());
- inboxholder.pushDateText.setText(new Date().toString());
- inboxholder.pushTimeText.setText(new Date().toString());
- inboxholder.messageText.setText((String)mItem.getMessage());
- }
- return view;
- }
- // For swipe action
- private View.OnClickListener onDeleteListener(final int position, final inboxHolder holder) {
- return new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AlertDialog.Builder alert = new AlertDialog.Builder(
- context);
- alert.setTitle("Delete Message");
- alert.setMessage("Are you sure you wish to delete this message?");
- alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- inboxList.remove(position);
- Gson gson = new Gson();
- inboxForSharedPref = fragment.getStoredMessages();
- inboxForSharedPref = inboxList;
- String jsonSavePref = gson.toJson(inboxForSharedPref);
- fragment.commitToStoredList(jsonSavePref);
- Toast.makeText(context, "Message Deleted!",
- Toast.LENGTH_SHORT).show();
- holder.swipeLayout.close();
- fragment.retrieveMessage();
- dialog.dismiss();
- }
- });
- alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- alert.show();
- }
- };
- }
- // Algorithm to filter out listview based on text changed in listview searchbox
- public void filter(String charText) {
- charText = charText.toLowerCase(Locale.getDefault());
- inboxList.clear();
- if (charText.length() == 0) {
- inboxList.addAll(inboxarraylist);
- }
- else
- {
- for (InboxBO ilist : inboxarraylist)
- {
- if (ilist.getDate().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getMessage().toLowerCase(Locale.getDefault()).contains(charText) || ilist.getSendername().toLowerCase(Locale.getDefault()).contains(charText))
- {
- inboxList.add(ilist);
- }
- }
- }
- notifyDataSetChanged();
- }
- public static class inboxHolder {
- TextView senderNameText, pushDateText, pushTimeText, messageText, delete, title;
- RelativeLayout itemLayout;
- private SwipeLayout swipeLayout;
- }
- // Methods below are for multi-deletion
- public void toggleSelection(int position) {
- selectView(position, !mSelectedItemIds.get(position));
- }
- // Remove selection after unchecked
- public void remove(InboxBO object) {
- inboxList.remove(object);
- Gson gson = new Gson();
- inboxForSharedPref = fragment.getStoredMessages();
- inboxForSharedPref = inboxList;
- String jsonSavePref = gson.toJson(inboxForSharedPref);
- fragment.commitToStoredList(jsonSavePref);
- notifyDataSetChanged();
- }
- // Item checked on selection
- public void selectView(int position, boolean value) {
- if (value)
- mSelectedItemIds.put(position, value);
- else
- mSelectedItemIds.delete(position);
- notifyDataSetChanged();
- }
- // Get number of selected item
- public int getSelectedCount() {
- return mSelectedItemIds.size();
- }
- public SparseBooleanArray getSelectedIds() {
- return mSelectedItemIds;
- }
- public void removeSelection() {
- mSelectedItemIds = new SparseBooleanArray();
- notifyDataSetChanged();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement