Skip to content Skip to sidebar Skip to footer

Showing Graphical Instead Of Strings In An Imagelist With Title

At the moment I am getting items out of my database and add them to a string called result which I return and set to my TextView: protected void onCreate(Bundle savedInstanceState)

Solution 1:

if i understood your question you need to create a customize an adapter.

Create a new simple class like this which holds an string and a picture

ClassObjectHolder{
      int Image;
      String Title;
    }

and create a getter and setter for this two

then create custom ArrayAdapter

    Class CustomArrayAdapter extendsArrayAdapter<ObjectHolder> {


      publicCustomArrayAdapter(Context C, ObjectHolder[] Arr) {
        super(C, R.layout.caa_xml, Arr);
      }

    @Overridepublic View getView(int position, View v, ViewGroup parent)
    {
    ViewmView= v ;
    if(mView == null){
        LayoutInflatervi= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = vi.inflate(R.layout.cpa_xml, null);
    }
    TextViewtext= (TextView) mView.findViewById(R.id.tv_caarow);
    ImageViewimage= (ImageView) mView.findViewById(R.id.iv_caarow);
    if(mView != null )
    {   text.setText(getItem(position).getText());
        image.setImageResource(getItem(position).getImage());
    return mView;
    }
    }

and create caa_xml.xml in res\layout\

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content" ><ImageViewandroid:id="@+id/iv_caarow"android:src="@drawable/icon"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tv_caarow"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingBottom="15dip"android:layout_BottomOf="@+id/iv_caarow" /></RelativeLayout>

and use it like this.

   GridView GV= (GridView) findViewById(R.Id.gv); // reference to xml or create in java
   ObjectHolder[] OHA;
   // assign your array, any ways!
   mAdapter CustomArrayAdapter= CustomArrayAdapter(this, OHA);
   GridView.setAdapter(mAdapter);

Post a Comment for "Showing Graphical Instead Of Strings In An Imagelist With Title"