Dhana
Dhana

Reputation: 554

How to delete button from GridView after click the button in Android?

I have add buttons in GridView, that buttons add programmatically, the number of buttons depend on word length each button has a character of word and hide that button when click, but i want to delete that when click it.

Here is the code

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

Upvotes: 0

Views: 457

Answers (1)

Snicolas
Snicolas

Reputation: 38168

Don't provide as many buttons as words.length. Use a different data structure, let's say a boolean array, that will hold wether or not each button has been clicked once already (all false at start).

Then when a button is clicked, toggle the boolean value.

When implementing the getCount method of your adapter, then loop through the array and count any flag indicating that a button still has to be shown.

Getview will be little more complex : you will receive an index, it will be the number of "false" in your array. So count them and get the right button to display.

Upvotes: 2

Related Questions