Android:表情符号键盘无法获取表情符号处理光标适配器

时间:2014-12-30 19:01:36

标签: android baseadapter android-cursoradapter emoji

我正在使用this Emoticon Keyboard。在项目中,作者使用BaseAdapterlink to code)在列表视图上呈现选定的表情符号。但对于我的应用程序,我使用的是CursorAdapter,这是唯一的区别。

一切正常,我可以显示表情符号弹出窗口,选择表情符号,然后显示在EditText中。我遇到的问题是,在我的应用程序中,我有Send按钮,当我点击它时,表情符号不会在列表视图上呈现(使用游标适配器),当然我必须遗漏一些东西。

从作者main example activity开始,我怀疑这是在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);

}

我尝试在游标适配器中执行相同的操作,但不是表情符号,而是显示一个奇怪的角色,这是我的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;
    }
}

与作者with baseadapter不同,可能是我做错了。或者可能应该在Spanned之间进行一些转换...

为了简化问题,我如何CursorAdapter而不是BaseAdapter上呈现表情符号(与作者不同)

当您查看我提供的示例代码的链接时,您将知道我的意思。

非常感谢您的帮助。感谢

1 个答案:

答案 0 :(得分:1)

我认为保存跨越到数据库可能存在一些错误。使用

保存跨区字符串
String htmlString = Html.toHtml(spannedText);

并使用

从数据库中获取相同的字符串
Spanned cs = Html.fromHtml(htmlString, imageGetter, null);

还有,

readEmoticons()中删除bindView(..)并在构造函数make imageGetter中调用它并在构造函数中初始化它。

希望它能解决您的问题。谢谢......