问题
I am using this Emoticon Keyboard. In the project author is using BaseAdapter
(link to code) to render selected emoticons on list view. But for my application I am using CursorAdapter
and this is the only difference.
Everything is working fine, I can display emoticon popup, select emoticons and they get displayed in EditText
. The problem Iam having is that in my app, I have Send
button and when I click on that, emoticons don't get rendered on the list view (using cursor adapter), naturally I must be missing something.
From the main example activity of author, I suspect this is where emoticons are read and displayed in EditText:
@Override
public void keyClickedIndex(final String index) {
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
StringTokenizer st = new StringTokenizer(index, ".");
Drawable d = new BitmapDrawable(getResources(),
emoticons[Integer.parseInt(st.nextToken()) - 1]);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned cs = Html.fromHtml("<img src ='" + index + "'/>", imageGetter,
null);
int cursorPosition = content.getSelectionStart();
content.getText().insert(cursorPosition, cs);
}
I tried to do same thing in my cursor adapter but instead of emoticon, an strange character shows up, here is my code for my CursorAdapter
:
public class MessageListAdapter extends CursorAdapter {
Context context;
private Bitmap[] emoticons;
private static final int NO_OF_EMOTICONS = 120;
private void readEmoticons() {
emoticons = new Bitmap[NO_OF_EMOTICONS];
for (short i = 0; i < NO_OF_EMOTICONS; i++) {
emoticons[i] = getImage((i + 1) + ".png");
}
}
@SuppressWarnings("deprecation")
public MessageListAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.context = context;
}
// bind views from db data for fields
@SuppressLint("InflateParams")
@Override
public void bindView(View view, final Context context, Cursor cursor) {
final int index = cursor.getPosition();
TextView lblDated = (TextView) view
.findViewById(R.id.lblDatedMessageList);
TextView lblTo = (TextView) view.findViewById(R.id.lblToMessageList);
lblDated.setText(Functions.getPrettyTime(cursor.getString(7)));
String message = cursor.getString(5);
readEmoticons();
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
StringTokenizer st = new StringTokenizer("" + index, ".");
Drawable d = new BitmapDrawable(context.getResources(),
emoticons[Integer.parseInt(st.nextToken()) - 1]);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned spanned = Html.fromHtml(message, imageGetter, null);
lblTo.setText(spanned);
}
/**
* For loading smileys from assets
*/
private Bitmap getImage(String path) {
AssetManager mngr = context.getAssets();
InputStream in = null;
try {
in = mngr.open("emoticons/" + path);
} catch (Exception e) {
e.printStackTrace();
}
Bitmap temp = BitmapFactory.decodeStream(in, null, null);
return temp;
}
}
May be I am doing above wrong unlike author with baseadapter. Or may be there should be some conversion between Spanned
stuff...
So to simplify question, how do I get emoticons rendered on CursorAdapter
instead of BaseAdapter
(unlike author) ?
When you look at links with example code I have provided, you will know what I mean.
Your help will be greatly appreciated. Thanks
回答1:
I think there might be some error in saving spanned to database. Save spanned string using
String htmlString = Html.toHtml(spannedText);
and get back the same string from database using
Spanned cs = Html.fromHtml(htmlString, imageGetter, null);
and also,
remove readEmoticons()
from bindView(..)
and call it in constructor make imageGetter
global and initialize it in constructor.
Hope it will solve your problem. Thanks...
来源:https://stackoverflow.com/questions/27711022/android-emoticon-keyboard-unable-to-get-emoticon-working-on-cursor-adapter