views:

9731

answers:

5

I have an application that will have 5-15 buttons depending on what is available from a backend. How do I define the proper GridView layout files to include an array of buttons that will each have different text and other attributes? Each button will essentially add an item to a cart, so the onClick code will be the same except for the item it adds to the cart.

How can I define an array so I can add a variable number of buttons, but still reference each of them by a unique ID? I've seen examples of the arrays.xml, but they have created an array of strings that are pre-set. I need a way to create an object and not have the text defined in the layout or arrays xml file.

Update - Added info about adding to a GridView

I want to add this to a GridView, so calling the addView method results in an UnsupportedOperationException. I can do the following:

ImageButton b2 = new ImageButton(getApplicationContext());
b2.setBackgroundResource(R.drawable.img_3);
android.widget.LinearLayout container = (android.widget.LinearLayout) findViewById(R.id.lay);
container.addView(b2);

but that doesn't layout the buttons in a grid like I would like. Can this be done in a GridView?

+3  A: 

Here's a nice sample for you:

http://developer.android.com/guide/tutorials/views/hello-gridview.html

You should just create buttons instead of imageviews in getView adapter method.

Fedor
A: 

Just changing them to buttons instead of imageviews didn't work for me. I am having trouble with this myself. It seems that whenever I try to assign an action to the listener of a button all of the buttons are effected. It is very confusing.

Dyce
A: 

Were you able solve the issue lately??/ I'm facing the same as of now.. :)

abhishek
+2  A: 

If you are using a GridView, or a ListView (etc), and are producing Views to populate them via the adapter getView(pos, convertView, viewGroup), you might encounter confusion (i did once).

If you decide to re-use the convertView parameter, you must reset everything inside of it. It is an old view being passed to you by the framework, in order to save the cost of inflating the layout. It is almost never associated with the position it was in the layout before.

class GridAdapter extends BaseAdapter // assigned to your GridView
{
    public View getView(int position, View convertView, ViewGroup arg2) {
        View view;
        if (convertView==null)
        {
            view = getLayoutInflater().inflate(R.layout.gd_grid_cell, null);
        }
        else
        {
             // reusing this view saves inflate cost
             // but you really have to restore everything within it to the state you want
            view = convertView;
        }


        return view;
    }
    //  other methods omitted (e.g. getCount, etc) 
}

I think this represents one of those Android things where the concept is a little difficult to grasp at first, until you realize there's a significant optimization available within it (have to be nice to CPU on a little mobile device)

alienjazzcat
+3  A: 

In the following code, you should change the upper limits of the for, to a variable.

public class MainActivity
        extends Activity
        implements View.OnClickListener {

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

            TableLayout layout = new TableLayout (this);
            layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );

            layout.setPadding(1,1,1,1);

            for (int f=0; f<=13; f++) {
                TableRow tr = new TableRow(this);
                for (int c=0; c<=9; c++) {
                    Button b = new Button (this);
                    b.setText(""+f+c);
                    b.setTextSize(10.0f);
                    b.setTextColor(Color.rgb( 100, 200, 200));
                    b.setOnClickListener(this);
                    tr.addView(b, 30,30);
                } // for
                layout.addView(tr);
            } // for

            super.setContentView(layout);
        } // ()

        public void onClick(View view) {
            ((Button) view).setText("*");
            ((Button) view).setEnabled(false);
        }
    } // class
cibercitizen1