/
RosterDialogsAdapter.java
127 lines (114 loc) Β· 4.6 KB
/
RosterDialogsAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.tomclaw.mandarin.main.adapters;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.tomclaw.mandarin.R;
import com.tomclaw.mandarin.core.GlobalProvider;
import com.tomclaw.mandarin.core.Settings;
import com.tomclaw.mandarin.util.StatusUtil;
/**
* Created with IntelliJ IDEA.
* User: solkin
* Date: 4/28/13
* Time: 9:54 PM
*/
public class RosterDialogsAdapter extends CursorAdapter implements
LoaderManager.LoaderCallbacks<Cursor> {
/**
* Adapter ID
*/
private static final int ADAPTER_DIALOGS_ID = -2;
/**
* Columns
*/
private static int COLUMN_ROSTER_BUDDY_ID;
private static int COLUMN_ROSTER_BUDDY_NICK;
private static int COLUMN_ROSTER_BUDDY_STATUS;
private static int COLUMN_ROSTER_BUDDY_ACCOUNT_TYPE;
/**
* Variables
*/
private Context context;
private LayoutInflater inflater;
public RosterDialogsAdapter(Activity context, LoaderManager loaderManager) {
super(context, null, 0x00);
this.context = context;
this.inflater = context.getLayoutInflater();
// Initialize loader for dialogs Id.
loaderManager.initLoader(ADAPTER_DIALOGS_ID, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
return new CursorLoader(context,
Settings.BUDDY_RESOLVER_URI, null, GlobalProvider.ROSTER_BUDDY_DIALOG + "='" + 1 + "'",
null, "(CASE WHEN " + GlobalProvider.ROSTER_BUDDY_STATUS + "=" + StatusUtil.STATUS_OFFLINE
+ " THEN 0 ELSE 1 END" + ") DESC," + GlobalProvider.ROSTER_BUDDY_NICK + " ASC");
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
// Detecting columns.
COLUMN_ROSTER_BUDDY_ID = cursor.getColumnIndex(GlobalProvider.ROSTER_BUDDY_ID);
COLUMN_ROSTER_BUDDY_NICK = cursor.getColumnIndex(GlobalProvider.ROSTER_BUDDY_NICK);
COLUMN_ROSTER_BUDDY_STATUS = cursor.getColumnIndex(GlobalProvider.ROSTER_BUDDY_STATUS);
COLUMN_ROSTER_BUDDY_ACCOUNT_TYPE = cursor.getColumnIndex(GlobalProvider.ROSTER_BUDDY_ACCOUNT_TYPE);
swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
swapCursor(null);
}
/**
* @see android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
try {
if (!getCursor().moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
if (convertView == null) {
v = newView(context, getCursor(), parent);
} else {
v = convertView;
}
bindView(v, context, getCursor());
} catch (Throwable ex) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.buddy_item, parent, false);
Log.d(Settings.LOG_TAG, "exception in getView: " + ex.getMessage());
}
return v;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
return inflater.inflate(R.layout.buddy_item, viewGroup, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Setup values
((TextView) view.findViewById(R.id.buddy_id)).setText(cursor.getString(COLUMN_ROSTER_BUDDY_ID));
((TextView) view.findViewById(R.id.buddy_nick)).setText(cursor.getString(COLUMN_ROSTER_BUDDY_NICK));
((ImageView) view.findViewById(R.id.buddy_status)).setImageResource(
StatusUtil.getStatusResource(
cursor.getString(COLUMN_ROSTER_BUDDY_ACCOUNT_TYPE),
cursor.getInt(COLUMN_ROSTER_BUDDY_STATUS)));
}
public int getBuddyDbId(int position) {
if (!getCursor().moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
return getCursor().getInt(getCursor().getColumnIndex(GlobalProvider.ROW_AUTO_ID));
}
}