简体   繁体   中英

Unable to add ImageButtons to gridView with adapter<ImageButton> in android

Im trying to make a grid view with ImageButtons dynamically and i have this problem. before starting i must say I'm new with android and java development (2.5 years of Objective-c).

Ok. so im using this code:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_background);

ArrayList<ImageButton> tmpStrRay = new ArrayList<ImageButton>();

Boolean load=true; 
for (int i= 0;load;i++){
    ImageButton iv = new ImageButton(this);
    InputStream ims;

    try {
        ims = getAssets().open("sm_backgrounds/bgSM_"+i+".jpg");
        Drawable d = Drawable.createFromStream(ims, null);
        iv.setImageDrawable(d);
        tmpStrRay.add(iv);

    } catch (IOException e) {
        load = false;
        e.printStackTrace();
    }

    System.out.print(i);
}

GridView grid = (GridView) findViewById(R.id.gridView);
ArrayAdapter<ImageButton> adapter = new ArrayAdapter<ImageButton>(this, android.R.layout.simple_list_item_1, tmpStrRay);
grid.setAdapter(adapter);

}

and i have this XML in my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BackgroundSelector" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" 
        android:id="@+id/topLinear">

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="close"
            android:text="Close"
            android:textSize="15sp" />

        <Button
            android:id="@+id/Button01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="showCamera"
            android:text="Camera"
            android:textSize="15sp" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/topLinear" >

        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="3" >
        </GridView>
    </ScrollView>

</RelativeLayout>

and I'm getting this strange result of Grid with text the normally i will say that describe the ImageButton instead of showing the button himself. I'm sure its an easy for the most of you - but please if you have the answer i would really like to get an explanation with it.

Thanks !!!

You use ArrayAdapter adapter which takes android.R.layout.simple_list_item_1 layout (which is TextView) and fills this textView with tmpStrRay[i].toString() text.

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM