Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 1 | package org.sflphone.adapters; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.sflphone.R; |
| 7 | import org.sflphone.model.SipMessage; |
| 8 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 9 | import android.content.Context; |
| 10 | import android.graphics.Bitmap; |
| 11 | import android.graphics.BitmapFactory; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 12 | import android.os.Bundle; |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 13 | import android.view.Gravity; |
| 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; |
| 16 | import android.view.ViewGroup; |
| 17 | import android.widget.BaseAdapter; |
| 18 | import android.widget.LinearLayout; |
| 19 | import android.widget.TextView; |
| 20 | |
| 21 | public class DiscussArrayAdapter extends BaseAdapter { |
| 22 | |
| 23 | private TextView countryName; |
| 24 | private List<SipMessage> messages = new ArrayList<SipMessage>(); |
| 25 | private LinearLayout wrapper; |
| 26 | private Context mContext; |
| 27 | |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 28 | public DiscussArrayAdapter(Context context, Bundle args) { |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 29 | mContext = context; |
Alexandre Lision | d568603 | 2013-10-29 11:09:21 -0400 | [diff] [blame] | 30 | |
| 31 | if(args == null) |
| 32 | messages = new ArrayList<SipMessage>(); |
| 33 | else |
| 34 | messages = args.getParcelableArrayList("messages"); |
| 35 | |
Alexandre Lision | 666b377 | 2013-10-28 17:42:48 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | public void add(SipMessage object) { |
| 39 | messages.add(object); |
| 40 | notifyDataSetChanged(); |
| 41 | } |
| 42 | |
| 43 | public int getCount() { |
| 44 | return this.messages.size(); |
| 45 | } |
| 46 | |
| 47 | public SipMessage getItem(int index) { |
| 48 | return this.messages.get(index); |
| 49 | } |
| 50 | |
| 51 | public View getView(int position, View convertView, ViewGroup parent) { |
| 52 | View row = convertView; |
| 53 | if (row == null) { |
| 54 | LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(mContext); |
| 55 | row = inflater.inflate(R.layout.item_message, parent, false); |
| 56 | } |
| 57 | |
| 58 | wrapper = (LinearLayout) row.findViewById(R.id.wrapper); |
| 59 | |
| 60 | SipMessage coment = getItem(position); |
| 61 | |
| 62 | countryName = (TextView) row.findViewById(R.id.comment); |
| 63 | |
| 64 | countryName.setText(coment.comment); |
| 65 | |
| 66 | countryName.setBackgroundResource(coment.left ? R.drawable.bubble_left_selector : R.drawable.bubble_right_selector); |
| 67 | wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT); |
| 68 | |
| 69 | return row; |
| 70 | } |
| 71 | |
| 72 | public Bitmap decodeToBitmap(byte[] decodedByte) { |
| 73 | return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public long getItemId(int position) { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | } |