Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I found out that I could set a callback for a View in the xml definition. Here's the updated xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:clickable="true" android:onClick="onClick" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dip" android:onClick="onCheckBoxClick"&gt;&lt;/CheckBox&gt; &lt;RelativeLayout android:layout_height="match_parent" android:id="@+id/relativeLayout1" android:layout_width="match_parent"&gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:layout_alignParentRight="true" android:text="Button" android:onClick="onButtonClick"&gt;&lt;/Button&gt; &lt;TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_toLeftOf="@+id/button1" android:layout_alignTop="@+id/button1" android:layout_width="match_parent"&gt;&lt;/TextView&gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="TextView" android:layout_below="@+id/textView1" android:layout_alignLeft="@+id/textView1" android:layout_alignRight="@+id/textView1"&gt;&lt;/TextView&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>I've updated the code with the appropriate callback hooks:</p> <pre><code>package com.camelconsultants.shoplist; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class HelloAndroid extends ListActivity { ArrayList&lt;HashMap&lt;String, String&gt;&gt; Items = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); GetData(); setListAdapter ( new SimpleAdapter ( this.getBaseContext(), Items, R.layout.multiitem, new String[]{ "Code", "Description" }, new int[]{ R.id.textView1, R.id.textView2 } ) ); ListView lv = getListView(); lv.setTextFilterEnabled( true ); } @SuppressWarnings("unchecked") private HashMap&lt;String, String&gt; getItem( View view ) { HashMap&lt;String, String&gt; Item = null; ListView listView = getListView(); int Position = listView.getPositionForView( view ); if ( listView.getItemAtPosition(Position) instanceof HashMap ) Item = (HashMap&lt;String, String&gt;)listView.getItemAtPosition( Position ); return Item; } public void onClick( View view ) { HashMap&lt;String, String&gt; Item = getItem( view ); if ( Item == null ) return; Toast.makeText ( getApplicationContext(), Item.get("Code") + ", " + Item.get( "Description" ), Toast.LENGTH_SHORT ).show(); } public void onCheckBoxClick( View view ) { HashMap&lt;String, String&gt; Item = getItem( view ); if ( Item == null ) return; Toast.makeText ( getApplicationContext(), "CheckBox! - " + Item.get("Code") + ", " + Item.get( "Description" ), Toast.LENGTH_SHORT ).show(); } public void onButtonClick( View view ) { HashMap&lt;String, String&gt; Item = getItem( view ); if ( Item == null ) return; Toast.makeText ( getApplicationContext(), "Button! - " + Item.get("Code") + ", " + Item.get( "Description" ), Toast.LENGTH_SHORT ).show(); } void GetData() { HashMap&lt;String, String&gt; Item; try { JSONObject JsonObject = new JSONObject( this.getResources().getString(R.string.Json) ); JSONArray JsonArray = JsonObject.getJSONArray( "Items" ); for ( int i = 0; i &lt; JsonArray.length(); i++ ) { Item = new HashMap&lt;String, String&gt;(); Item.put( "Code", JsonArray.getJSONObject(i).getString("Code") ); Item.put( "Description", JsonArray.getJSONObject(i).getString("Description") ); Items.add( Item ); } } catch( JSONException Bummer ) { Bummer.printStackTrace(); } } } </code></pre> <p>The <code>getItem</code> function returns the HashMap for the current item by passing up the View to <code>getItemAtPosition</code>. The cool thing about that is that it doesn't matter whether the view is nested or not. </p> <p>However, I'm assuming that creating my own Adapter and using the setTag method is more efficient. But for the time being, the above code suits my purposes!</p>
 

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