ListItemAdapter.java

 1package eu.siacs.conversations.ui.adapter;
 2
 3import java.util.List;
 4
 5import eu.siacs.conversations.Config;
 6import eu.siacs.conversations.R;
 7import eu.siacs.conversations.entities.ListItem;
 8import eu.siacs.conversations.ui.XmppActivity;
 9import eu.siacs.conversations.xmpp.jid.Jid;
10
11import android.content.Context;
12import android.content.SharedPreferences;
13import android.preference.PreferenceManager;
14import android.util.Log;
15import android.view.LayoutInflater;
16import android.view.View;
17import android.view.ViewGroup;
18import android.widget.ArrayAdapter;
19import android.widget.ImageView;
20import android.widget.LinearLayout;
21import android.widget.TextView;
22
23public class ListItemAdapter extends ArrayAdapter<ListItem> {
24
25	protected XmppActivity activity;
26	protected boolean showDynamicTags = false;
27	private View.OnClickListener onTagTvClick = new View.OnClickListener() {
28		@Override
29		public void onClick(View view) {
30			if (view instanceof  TextView && mOnTagClickedListener != null) {
31				TextView tv = (TextView) view;
32				final String tag = tv.getText().toString();
33				mOnTagClickedListener.onTagClicked(tag);
34			}
35		}
36	};
37	private OnTagClickedListener mOnTagClickedListener = null;
38
39	public ListItemAdapter(XmppActivity activity, List<ListItem> objects) {
40		super(activity, 0, objects);
41		this.activity = activity;
42		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
43		this.showDynamicTags = preferences.getBoolean("show_dynamic_tags",false);
44	}
45
46	@Override
47	public View getView(int position, View view, ViewGroup parent) {
48		LayoutInflater inflater = (LayoutInflater) getContext()
49				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
50		ListItem item = getItem(position);
51		if (view == null) {
52			view = inflater.inflate(R.layout.contact, parent, false);
53		}
54		TextView tvName = (TextView) view.findViewById(R.id.contact_display_name);
55		TextView tvJid = (TextView) view.findViewById(R.id.contact_jid);
56		ImageView picture = (ImageView) view.findViewById(R.id.contact_photo);
57		LinearLayout tagLayout = (LinearLayout) view.findViewById(R.id.tags);
58
59		List<ListItem.Tag> tags = item.getTags();
60		if (tags.size() == 0 || !this.showDynamicTags) {
61			tagLayout.setVisibility(View.GONE);
62		} else {
63			tagLayout.setVisibility(View.VISIBLE);
64			tagLayout.removeAllViewsInLayout();
65			for(ListItem.Tag tag : tags) {
66				TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag,tagLayout,false);
67				tv.setText(tag.getName());
68				tv.setBackgroundColor(tag.getColor());
69				tv.setOnClickListener(this.onTagTvClick);
70				tagLayout.addView(tv);
71			}
72		}
73		final Jid jid = item.getJid();
74		if (jid != null) {
75			tvJid.setText(jid.toString());
76		} else {
77			tvJid.setText("");
78		}
79		tvName.setText(item.getDisplayName());
80		picture.setImageBitmap(activity.avatarService().get(item,
81				activity.getPixel(48)));
82		return view;
83	}
84
85	public void setOnTagClickedListener(OnTagClickedListener listener) {
86		this.mOnTagClickedListener = listener;
87	}
88
89	public interface OnTagClickedListener {
90		public void onTagClicked(String tag);
91	}
92
93}