安卓:表情键盘无法获得的表情光标适配器上工作 [英] Android: Emoticon Keyboard unable to get emoticon working on cursor adapter
问题描述
我使用这个表情键盘。在该项目中笔者使用 BaseAdapter
(<一个href=\"https://github.com/chiragjain/Emoticons-Keyboard/blob/master/src/com/learn/testdifferentkeyboard/EmoticonsGridAdapter.java\"相对=nofollow>链接到code )呈现在列表视图中选择表情符号。但是,对于我的应用程序正在使用的CursorAdapter
,这是唯一的区别。
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.
一切工作正常,我可以显示表情图标弹出,选择表情和他们得到显示在的EditText
。有荫的问题是,在我的应用程序,我有发送
按钮,当我点击上,表情没有得到列表视图渲染(使用光标适配器),当然我必须失去了一些东西。
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.
从<一个href=\"https://github.com/chiragjain/Emoticons-Keyboard/blob/master/src/com/learn/testdifferentkeyboard/MainActivity.java\"相对=nofollow>作者的主要的例子活动,我怀疑这是哪里的表情被读出并在显示的EditText:
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);
}
我试图做同样的事情在我的光标适配器,但不是表情,一个奇怪的字符显示出来,这是我的code为我的的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;
}
}
可能是我做的不对以上笔者不同的与baseadapter 。或者可能是应该有跨区
的东西...
May be I am doing above wrong unlike author with baseadapter. Or may be there should be some conversion between Spanned
stuff...
因此,为了简化问题,我怎么获得上呈现的的CursorAdapter
表情符号代替 BaseAdapter
(不像作者)?
So to simplify question, how do I get emoticons rendered on CursorAdapter
instead of BaseAdapter
(unlike author) ?
当你看到我已经提供了例如code链接,你就会明白我的意思。
When you look at links with example code I have provided, you will know what I mean.
您的帮助将大大AP preciated。谢谢
Your help will be greatly appreciated. Thanks
推荐答案
我觉得储蓄跨越数据库可能会有一些错误。使用保存跨区串
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);
和还
删除 readEmoticons()
从 bindView(..)
并调用它的构造函数生成 imageGetter
全球,并在构造函数初始化。
remove readEmoticons()
from bindView(..)
and call it in constructor make imageGetter
global and initialize it in constructor.
希望这本书能解决你的问题。谢谢...
Hope it will solve your problem. Thanks...
这篇关于安卓:表情键盘无法获得的表情光标适配器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!