2012-07-12 82 views
0

我在GridView中添加按鈕,按鈕以編程方式添加,按鈕的數量取決於字長每個按鈕都有一個字的字符,並單擊時隱藏該按鈕,但我想單擊時刪除它。如何在單擊Android中的按鈕後從GridView中刪除按鈕?

下面是代碼

SpellAdapter.java

public class SpellAdapter extends BaseAdapter{ 


    public Context context; 
    public char[] word; 
     public String spellWord1; 
    public SpellAdapter(Context context, char[] word, String orglWord) 
    { 
     this.context=context; 
     this.word=word; 
     spellWord1 = orglWord; 
    } 

    public int getCount() { 
     count=word.length; 
     return count; 
    } 

    public Object getItem(int position) { 

     return null; 
    } 

    public long getItemId(int position) { 

     return 0; 
    } 


    public View getView(final int position, View convertView, ViewGroup arg2) { 

     View v = convertView; 
     if (v == null) 
     { 
      LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.buttonlist, null); 
     } 

     final Button btn= (Button)v.findViewById(R.id.letterbtn); 

     btn.setText(word[position]+""); 

     btn.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) 
      { 
       letters=btn.getText(); 
       String word = letters.toString(); 
       btn.setVisibility(View.GONE); // Here invisible the button. 

      } 


     }); 

     return v; 
    } 
} 

回答

2

不提供儘可能多的按鈕爲words.length。使用一個不同的數據結構,讓我們說一個布爾數組,它會保持或不是每個按鈕已經被點擊過一次(全部在開始時都是錯誤的)。

然後當點擊一個按鈕時,切換布爾值。

實現適配器的getCount方法時,然後遍歷數組並計算任何標誌,指示仍然必須顯示一個按鈕。

Getview會稍微複雜一些:您將收到一個索引,它將是數組中「false」的數量。所以數數並獲得正確的按鈕來顯示。

相關問題