Note that there are some explanatory texts on larger screens.

plurals
  1. POClicking an ImageButton inside a ListView
    text
    copied!<p>I have a ListView. Each row is a custom layout having some textviews and 2 imagebuttons. Every tutorial/topic/documentation/etc. i tried, nothing worked on me, even thou testing the tutorials worked perfect on their own. So, i populate the ListView, than i'd like that by clicking an imagebutton on the row x to at least show me a toast or do something, like it should.</p> <p>Below is the code (MainActivity.java).</p> <pre><code>package ro.pca.pizzarecipes; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import com.google.ads.AdRequest; import com.google.ads.AdView; import android.os.Bundle; import android.app.Activity; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity{ protected AdView adView; public ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdRequest re = new AdRequest(); re.setGender(AdRequest.Gender.FEMALE); adView = (AdView)findViewById(R.id.ad); adView.setVisibility(AdView.VISIBLE); adView.loadAd(re); ArrayList&lt;HashMap&lt;String, String&gt;&gt; map = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); HashMap mapItem1 = new HashMap(); mapItem1.put("Nume","Neapolitan style (alla Napoletana)"); mapItem1.put("Timp","30"); mapItem1.put("Calorii",""); mapItem1.put("Ingrediente","1 1/2 cups warm water..."); mapItem1.put("Preparare"," 1. In the bowl..."); mapItem1.put("Altele","Makes 8 servings."); map.add(mapItem1); HashMap mapItem2 = new HashMap(); mapItem2.put("Nume","Pan fried Hawaiian"); mapItem2.put("Timp","20"); mapItem2.put("Calorii",""); mapItem2.put("Ingrediente","Olive oil..."); mapItem2.put("Preparare"," Preheat..."); mapItem2.put("Altele","Makes 2 pizzas."); map.add(mapItem2); SimpleAdapter adapter = null; adapter = new SimpleAdapter(this, map, R.layout.customlistlayout, new String[]{"Nume", "Timp", "Calorii", "Ingrediente", "Preparare", "Altele"}, new int[]{R.id.textView1, R.id.textView2, R.id.textView4, R.id.textView7, R.id.textView9, R.id.textView10}); Collections.sort(map, new Comparator&lt;Map&lt;String, String&gt;&gt;() { @Override public int compare(Map&lt;String, String&gt; o1, Map&lt;String, String&gt; o2) { String name1 = o1.get("Nume"); String name2 = o2.get("Nume"); if (name1 == null) return -1; return name1.compareTo(name2); } }); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.listView1); lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE); lv.setAdapter(adapter); } } </code></pre> <p>the main layout (activity_main.xml)</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:orientation="vertical" &gt; &lt;com.google.ads.AdView android:id="@+id/ad" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adSize="SMART_BANNER" ads:adUnitId="a150d19cc5b81d8" ads:loadAdOnCreate="true" /&gt; &lt;/LinearLayout&gt; &lt;Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout1" android:text="my fridge" /&gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:divider="#C60202" android:dividerHeight="3dip" android:layout_below="@+id/button1" &gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>the row layout (customlistlayout.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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="italic" /&gt; &lt;TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_toLeftOf="@+id/textView2" android:text="time (min):" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/textView2" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="italic" /&gt; &lt;TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView4" android:layout_alignBottom="@+id/textView4" android:layout_toLeftOf="@+id/textView4" android:text="calories:" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView5" android:text="ingredients" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="bold|italic" /&gt; &lt;TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView6" android:layout_marginLeft="18dp" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="14sp" /&gt; &lt;TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView7" android:text="preparation" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="bold|italic" /&gt; &lt;TextView android:id="@+id/textView9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView7" android:layout_below="@+id/textView8" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="14sp" /&gt; &lt;ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/imageButton1" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/ic_launcher" android:tag="2" /&gt; &lt;TextView android:id="@+id/textView11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView9" android:text="other info:" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="bold|italic" /&gt; &lt;ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView10" android:layout_marginTop="20dp" android:focusable="false" android:focusableInTouchMode="false" android:src="@drawable/googletranslate" android:tag="1" /&gt; &lt;TextView android:id="@+id/textView10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView9" android:layout_below="@+id/textView11" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="15sp" android:textStyle="italic" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:background="@layout/gradient" android:text="nume" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="30sp" android:textStyle="bold" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>and a little gradient layout (gradient.xml)</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"&gt; &lt;gradient android:startColor="#A0C2F2" android:endColor="#D0E6F2" android:angle="135" /&gt; &lt;/shape&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