Back to project page MySms.
The source code is released under:
Apache License
If you think the Android project MySms listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.henningta.mysms; //from w w w. ja v a 2 s . com import android.content.Context; import android.graphics.Typeface; import android.os.Build; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; /** * Custom adapter for SMS conversations on main page */ public class ConversationAdapter extends ArrayAdapter<Conversation> { private DatabaseHandler dbHandler; private LayoutInflater inflater; private int resourceId; private HashSet<Conversation> checkedItems; private boolean multiMode; public ConversationAdapter(Context context, int resourceId, ArrayList<Conversation> conversations) { super(context, resourceId, conversations); this.dbHandler = new DatabaseHandler(context); this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.resourceId = resourceId; this.checkedItems = new HashSet<Conversation>(); this.multiMode = false; } @Override public View getView(int position, View convertView, ViewGroup parent) { ItemHolder holder; if (convertView == null) { // create new holder = new ItemHolder(); convertView = inflater.inflate(resourceId, parent, false); holder.name = (TextView)convertView.findViewById(R.id.conversation_text_title); holder.count = (TextView)convertView.findViewById(R.id.conversation_text_count); holder.subtitle = (TextView)convertView.findViewById(R.id.conversation_text_subtitle); holder.time = (TextView)convertView.findViewById(R.id.conversation_text_time); convertView.setTag(holder); } else { // recycle holder = (ItemHolder)convertView.getTag(); } /* find conversation by position */ Conversation conversation = this.getItem(position); /* get last message in conversation or set default values (should never happen) */ String subtitle = "No messages."; ArrayList<Message> messages = dbHandler.getMessages(conversation.getSource()); Message message = null; if (messages.size() > 0) { message = messages.get(messages.size() - 1); /* get message text */ subtitle = message.getText(); } long time = conversation.getTime(); /* format time */ String formattedTime = ""; if (time == 0 || time == Settings.NOT_SET) holder.time.setVisibility(View.GONE); else formattedTime = Settings.formatTime(time); /* set values */ holder.name.setText(conversation.getName()); holder.count.setText(String.valueOf(messages.size())); holder.subtitle.setText(subtitle); if (message != null) { if (message.getStatus() == Message.STATUS_SENDING) { holder.time.setText("Sending..."); } else if (message.getStatus() == Message.STATUS_FAILED) { holder.time.setText("Failed."); } else { holder.time.setText(formattedTime); } } /* set default styles */ convertView.setBackgroundResource(R.drawable.conversation_selector_default); holder.name.setTypeface(null, Typeface.NORMAL); holder.count.setTypeface(null, Typeface.NORMAL); /* set styles for when conversation contains new messages */ if (conversation.hasNewMessages()) { convertView.setBackgroundResource(R.drawable.conversation_selector_has_new); holder.name.setTypeface(null, Typeface.BOLD); holder.count.setTypeface(null, Typeface.BOLD); } /* set checked state */ if (checkedItems.contains(conversation)) { // if this item is checked convertView.getBackground().setState(new int[] { android.R.attr.state_checked }); } else { // if this item is unchecked (notice the minus) convertView.getBackground().setState(new int[] { -android.R.attr.state_checked }); } return convertView; } @Override public void addAll(Collection<? extends Conversation> items) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { for (Conversation item : items) { this.add(item); } } else { super.addAll(items); } } /** * Holder class for conversation resources */ class ItemHolder { TextView name; TextView count; TextView subtitle; TextView time; } /** * Begin multi-mode selection of items */ public void enterMultiMode() { multiMode = true; notifyDataSetChanged(); } /** * Stop selecting items */ public void exitMultiMode() { checkedItems.clear(); multiMode = false; notifyDataSetChanged(); } /** * Set item at current position to checked or unchecked */ public void setChecked(int position, boolean checked) { Conversation conversation = this.getItem(position); if (checked) checkedItems.add(conversation); else checkedItems.remove(conversation); if (multiMode) notifyDataSetChanged(); } /** Return checked state of item at position */ /*public boolean isChecked(int position) { return checkedItems.contains(this.getItem(position)); }*/ /** * Toggle checked state of item at position */ public void toggleChecked(int position) { final Conversation conversation = this.getItem(position); if (checkedItems.contains(conversation)) checkedItems.remove(conversation); else checkedItems.add(conversation); notifyDataSetChanged(); } /** * Return number of checked items */ public int getCheckedItemCount() { return checkedItems.size(); } /** * Return set of checked items */ public Set<Conversation> getCheckedItems() { return checkedItems; } }