Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid- Populate custom Views in GridView
    primarykey
    data
    text
    <p>I'm developing an Android Application where I need to create a custom view and have to populate that view repeatedly in Grid View . </p> <p>Here is my code snippets for creating the view</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;LinearLayout android:id="@+id/layoutContent" android:layout_width="80dp" android:layout_height="80dp" android:orientation="horizontal" android:background="@drawable/navigationtextheaderbg"&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>The Gridview Adapter is given below,</p> <pre><code>class GridViewAdapter extends BaseAdapter{ Context c; int[] imageResources; String[] arrayTexts; GridViewAdapter(Context x,int[] imageResourses, String[] array ) { this.c=x; this.imageResources=imageResourses; this.arrayTexts=array; } @Override public int getCount() { // TODO Auto-generated method stub return arrayTexts.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View v=View.inflate(c,R.layout.grid,null); TextView tv1=(TextView)v.findViewById(R.id.textView1); ImageView iv1=(ImageView)v.findViewById(R.id.imageView1); tv1.setText(arrayTexts[position]); iv1.setBackgroundResource(imageResources[position]); return v; } } </code></pre> <p>I'm calling this Adapter using the following code snippets,</p> <pre><code>GridView gridView=new GridView(MainActivity.this); gridView.setNumColumns(4); GridViewAdapter adapter=new GridViewAdapter(MainActivity.this,imageresources,arr1); gridView.setAdapter(adapter); </code></pre> <p>here <strong>imageresourses &amp; arr1</strong> is an array of <strong>Images and string</strong> respectively .</p> <pre><code>int[] imageresources= new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; String[] arr1={"Ima","hello","yes","no"}; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload