Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest thing to do is use an <code>ArrayAdapter&lt;MyDataObject&gt;</code> where </p> <pre><code> public class MyDataObject { public String string1; public String string2; // any other useful attributes } </code></pre> <p>And then you would change <code>items</code> to a <code>MyDataObject[] items</code> stored in your class, and instead of doing <code>super.getView(index)</code> you'd do <code>items[index]</code> (which would yield a <code>MyDataObject</code>) and use that data instead.</p> <p>Also, importantly: you should <a href="http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html" rel="nofollow">use the convertView</a>. And possibly the <a href="http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/" rel="nofollow">ViewHolder pattern</a>.</p> <p><strong>Edit</strong>: At OP's request, a little more elaboration. Note that this uses the convertView pattern but not the ViewHolder pattern (you should be able to adopt that fairly easily).</p> <p>In your <code>Adapter</code>, you'd change <code>getView()</code> as follows:</p> <pre><code>public View getView(int position, View convertView, ViewGroup parent) { ViewGroup row; if(convertView == null){ // create your view here. row = getLayoutInflater().inflate(R.layout.row); } else { row = convertView; } // note: when you implement ViewHolder, the ViewHolder will // hold this reference so that you don't need to look it up every time. ImageView icon=(ImageView)row.findViewById(R.id.icon); // here you're employing the "items" array that you were using // before, except now it contains MyDataObjects. pick out the // string (or other data you want to check) from the resulting MyDataObject, // and see if it's longer than 4 characters. MyDataObject objectAtThisPosition = items[position]; if (objectAtThisPosition.string1.length()&gt;4) { icon.setImageResource(R.drawable.delete); } else { icon.setImageResource(R.drawable.ok); } // Do whatever else you want to with objectAtThisPosition. return(row); } </code></pre> <p>That's it for the easy way, and quite similar to what you have.</p> <p>Some more detail; if you don't care, skip it. :)</p> <p>I know that Adapters can seem magical, so in the interest of showing how <code>ListView</code> adapters work, here's an example using a <code>List</code> instead of an <code>Array</code>, so we can remove any magic that <code>ArrayAdapter</code> does with the array behind the scenes. I use a <code>List</code> because they can be more versatile for whatever you're trying to accomplish (<code>ArrayList</code> or <code>LinkedList</code> or what-have-you).</p> <p>To use a <code>List</code> you'd have the following in your <code>Activity</code>:</p> <pre><code> private List&lt;MyDataObject&gt; myList = new ArrayList&lt;MyDataObject&gt;(); </code></pre> <p>And instead of <code>items[position]</code> you'd use </p> <pre><code> MyDataObject objectAtThisPosition = myList.get(position); </code></pre> <p>If you want to change your data set dynamically, you should probably use this approach (keeping <code>myList</code> at the <code>Activity</code> level) instead of using an Array and an ArrayAdapter. That would mean you'd need to change from extending <code>ArrayAdapter&lt;String&gt;</code> to just extending <code>BaseAdapter&lt;MyDataObject&gt;</code> (most of the methods in <code>BaseAdapter</code> are trivial to implement) since our data size, for example, would be determined by our list, and not the <code>ArrayAdapter</code>'s array.</p> <p>I know that's kind of a fire hose, but let me know if you have any questions!</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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