Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So here's the thing. Anything except a single string per record in grids require something special. This is not perfect code by any means, but it will help.</p> <p>Let's start with your Base Layout. </p> <p>Let's say this is in something called mainlist.xml</p> <pre><code>&lt;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" &gt; &lt;ListView android:id="@+id/itemList" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>So here you have a Relative with a ListView. Pretty basic right?</p> <p>In your activity you will need some code like this.</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainlist); GetAndAssociateData(); } public void GetAndAssociateData() { ListView list = (ListView)findViewById(R.id.itemList); final ArrayList&lt;CountedItem&gt; items; list.setAdapter(new CountedItemAdapter(getApplicationContext(), R.id.countedItemRow, items, this)); } </code></pre> <p>Anything outside of that single string item requires not only an adapater, but a layout for each item.</p> <p>CountedItemAdapter is the adapter in this case (it tells how how to populate the view).</p> <p>countedItemRow is layout for each item.</p> <p>/src/CountedItemAdapter.java</p> <pre><code>public CountedItemAdapter(Context context,int textViewResourceId, ArrayList&lt;CountedItem&gt; objects,CountedItemActivity parentActivity) { super(context, textViewResourceId, objects); this.context = context; this.items = objects; this.parentActivity = parentActivity; // TODO Auto-generated constructor stub } public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.countedItemRow, null); } if (items != null){ CountedItem item = items.get(position); if (item != null) { TextView thing= (TextView) view.findViewById(R.id.thing); if (thing!= null) { thing.setText(item.GetThingText()); } TextView thingCount = (TextView) view.findViewById(R.id.thingCount); if (thingCount != null) { thingCount .setText(item.GetThingCount()); } } } return view; } </code></pre> <p>/res/layout/countedItemRow.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/countedItemRow" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/thing" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="0dp" android:layout_y="0dp" /&gt; &lt;TextView android:id="@+id/thingCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="0dp" android:layout_y="150dp" /&gt; &lt;/RelativeLayout&gt; </code></pre>
 

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