2013-12-13 17 views
0

刪除事件在我的應用程序已經使用的芯片使用Chips。而且它工作正常。當用戶添加聯繫人時,其ID將保存在數組列表中。問題是當用戶使用軟鍵盤上的刪除按鈕刪除聯繫人時,我將如何從arraylist中刪除其ID。如何將芯片連接到它的ID。請幫助我。提前感謝。爲聯繫人選擇獲取芯片的Android

以下代碼用於創建芯片。

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()); 
    } 

} 

回答

0

我努力理解你的問題,我看不出你的代碼示例是如何與你的「我將如何從ArrayList中刪除其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匹配的名稱。

+0

其實我只是在arraylist.When添加在編輯文字的名稱和ID的用戶刪除我想刪除其ID名稱。 – user1767260