4

我正在尝试在编辑文本中显示图像和文本,但它不适用于冰淇淋三明治和软糖。它在以下版本中运行良好。

当活动暂停并再次恢复时,它会在编辑文本中显示图像名称而不是图像以及文本..

请参考图片和相关代码

 public class MainActivity extends Activity implements OnClickListener,
        OnItemClickListener {

     String path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/Emoticons";


HashMap<String, Integer> emoticons = new LinkedHashMap<String, Integer>();

ArrayList<String> arrayList = new ArrayList<String>();

Bitmap bitmap;

EditText editText;

ImageButton cat_one;

ArrayList<String> name = new ArrayList<String>();

View view;

CharSequence[] namesList = { "Email"};

Dialog dialog;

GridView gridView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {

        File sd = new File(path);

        if (!sd.exists())
            sd.mkdirs();

    }


    addComponents();

    addListeners();
}

private void fillArrayList() {

    Iterator<Entry<String, Integer>> iterator = emoticons.entrySet()
            .iterator();

    Log.d("Iterator", iterator.toString());

    while (iterator.hasNext()) {

        Entry<String, Integer> entry = iterator.next();

        Log.d("Entry", entry.getKey());

        arrayList.add(entry.getKey());

    }
}

private void getCategory(String cat_name) {
    // TODO Auto-generated method stub

    if (name.size() > 0)
        name.clear();

    if (emoticons.size() > 0)
        emoticons.clear();

    if (arrayList.size() > 0)
        arrayList.clear();

    Field names[] = R.drawable.class.getFields();

    for (Field f : names) {

        if (f.getName().contains(cat_name)) {

            name.add(f.getName());

        }

    }

    for (int i = 0; i < name.size(); i++) {

        int id = getResources().getIdentifier(name.get(i), "drawable",
                getPackageName());

        Log.d("Drawable Folder", name.get(i).toString());
        emoticons.put(name.get(i), id);

    }
    fillArrayList();
}

private void addListeners() {
    // TODO Auto-generated method stub

    cat_one.setOnClickListener(this);

    gridView.setOnItemClickListener(this);
}

private void addComponents() {
    // TODO Auto-generated method stub


    editText = (EditText) findViewById(R.id.editText1);

    editText.requestFocus();

    view = getLayoutInflater().inflate(R.layout.popup, null);

    gridView = (GridView) view.findViewById(R.id.gridLayout);

    dialog = new Dialog(this, R.style.DialogSlideAnim);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    dialog.getWindow().setGravity(Gravity.BOTTOM);

    Window window = dialog.getWindow();

    window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    dialog.setContentView(view);

    dialog.setCanceledOnTouchOutside(true);


    cat_one = (ImageButton) findViewById(R.id.category_one);



}

public Spannable getSmiledText(Context context, String text) {

    SpannableStringBuilder builder = new SpannableStringBuilder(text);

    int index;

    for (index = 0; index < builder.length(); index++) {

        for (Entry<String, Integer> entry : emoticons.entrySet()) {

            int length = entry.getKey().length();

            if (index + length > builder.length())

                continue;

            if (builder.subSequence(index, index + length).toString()
                    .equals(entry.getKey())) {

                builder.setSpan(new ImageSpan(context, entry.getValue()),
                        index, index + length,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                index += length - 1;

                break;

            }
        }
    }
    return builder;
}

class ImageAdapter extends BaseAdapter {

    public int getCount() {
        return arrayList.size();
    }

    public Object getItem(int position) {
        return arrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.row, null);
        ImageView imageView = (ImageView) convertView
                .findViewById(R.id.imageView1);

        imageView.setBackgroundDrawable(getResources().getDrawable(
                emoticons.get(arrayList.get(position))));

        Log.d(getLocalClassName(),
                String.valueOf(emoticons.get(arrayList.get(position))));

        return convertView;
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


    if (v == cat_one) {


        getCategory("good");

        gridView.setAdapter(new ImageAdapter());

    }

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub

    String value = gridView.getAdapter().getItem(position).toString();

    Spannable spannable = getSmiledText(MainActivity.this, value);

    SpannableStringBuilder ssb = new SpannableStringBuilder(
            editText.getText()).append(spannable).append(" ");

    editText.setText(ssb);
    editText.setSelection(editText.getText().length());
    editText.setCursorVisible(true);
}
  }   

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

3

我认为我们需要进行一些跟踪才能更好地了解出了什么问题。但是由于您无法加载图像,我想知道是否可能是您无法访问外部存储目录

于 2012-12-20T23:13:44.573 回答
2

我建议在加载图像时使用Asynctask,这将解决您的问题,我遇到了同样的问题。

http://developer.android.com/reference/android/os/AsyncTask.html

http://android-developers.blogspot.mx/2010/07/multithreading-for-performance.html

使用 AsyncTask 在 ListView 中加载图片

于 2012-12-21T00:15:32.763 回答