在Android中获取芯片删除事件

时间:2013-12-13 04:09:10

标签: android android-edittext

在我的应用程序中,我使用Chips使用芯片进行联系人选择。并且工作正常。当用户添加联系人时,其ID将保存在arraylist中。问题是当用户使用软键盘中的删除按钮删除联系人时,如何从arraylist中删除其ID。如何将achip链接到其id.Please帮助我。谢谢。

以下代码用于创建芯片。

public void setChips() {
    if (friends_multiautocomplete_tv.getText().toString().contains(";")) // check
                                                                            // semicolon
                                                                            // in
                                                                            // string
    {

        SpannableStringBuilder ssb = new SpannableStringBuilder(
                friends_multiautocomplete_tv.getText());
        // split string with semicolon
        String chips[] = friends_multiautocomplete_tv.getText().toString()
                .trim().split(";");
        int x = 0;
        // loop will generate ImageSpan for every name separated by
        // semicolon
        for (String c : chips) {
            // inflate chips_edittext layout
            LayoutInflater lf = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            TextView textView = (TextView) lf.inflate(
                    R.layout.chips_edittext, null);
            textView.setText(c); // set text
            Typeface tfNormal = Typeface.createFromAsset(getAssets(),
                    "SourceSansPro-Regular.ttf");
            textView.setTypeface(tfNormal);
            // capture bitmapt of genreated textview
            int spec = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            textView.measure(spec, spec);
            textView.layout(0, 0, textView.getMeasuredWidth(),
                    textView.getMeasuredHeight());
            Bitmap b = Bitmap.createBitmap(textView.getWidth(),
                    textView.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(b);
            canvas.translate(-textView.getScrollX(), -textView.getScrollY());
            textView.draw(canvas);
            textView.setDrawingCacheEnabled(true);
            Bitmap cacheBmp = textView.getDrawingCache();
            Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
            textView.destroyDrawingCache(); // destory drawable
            // create bitmap drawable for imagespan
            BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
            bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(),
                    bmpDrawable.getIntrinsicHeight());
            // create and set imagespan
            ssb.setSpan(new ImageSpan(bmpDrawable), x, x + c.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            x = x + c.length() + 1;
        }
        // set chips span
        friends_multiautocomplete_tv.setText(ssb);
        // move cursor to last
        friends_multiautocomplete_tv
                .setSelection(friends_multiautocomplete_tv.getText()
                        .length());
    }

}

1 个答案:

答案 0 :(得分:0)

我很难理解你的问题,但我没看到你的代码示例与你的问题有什么关系?我怎样才能从arraylist中删除它的id"但一般情况下,您想要遍历数组列表,检查当前项是否与您要删除的ID匹配:

如果您尝试匹配多个项目,则可以创建一个将两个或多个变量关联在一起的结构。然后,您可以存储此结构,如果存储您的ID或具有帮助您将名称与ID匹配的其他结构。例如:

public class DataStructure{
    public String id;
    public String name;
}

ArrayList<DataStructure> arrayList = null;
String name;

for(DataStructure item : arrayList) {
    if(item.name.equals(name)) {
        arrayList.remove(item);
        break;
    }
}

请注意,在任何情况下,如果您尝试将姓名与ID匹配,很可能会有多个ID与姓名匹配。