0

I would like to set onclick event for Button in list item of ListView

I have a layout ("list_item.xml") in this way:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow android:layout_width="fill_parent"
    android:id="@+id/TableRow_list_item"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:id="@+id/imageView_logo_utente"
        android:src="@drawable/icon_user_green"
        android:layout_column="1"
        android:layout_weight="1"/>

    <TextView
        android:textColor="#000000"
        android:id="@+id/nomec"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Nome Completo"
        android:layout_weight="2"
        android:gravity="center"
        android:height="40dp"
        android:textSize="17dp"
        android:layout_column="1"/>

    <TextView
        android:textColor="#000000"
        android:id="@+id/tiposogget"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Tipo Soggetto Etichetta"
        android:layout_weight="2"
        android:gravity="center"
        android:height="40dp"
        android:textSize="17dp"
        android:layout_column="1"/>

    <TextView
        android:textColor="#000000"
        android:id="@+id/cf"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Codice Fiscale"
        android:gravity="center"
        android:height="40dp"
        android:textSize="17dp"
        android:layout_column="1"
        android:layout_weight="2" />


    <TextView
        android:textColor="#000000"
        android:id="@+id/idsogg"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ID Soggetto"
        android:layout_weight="1"
        android:gravity="center"
        android:height="40dp"
        android:textSize="17dp"
        android:layout_column="1"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="90dp"
        android:id="@+id/button_scan"
        android:layout_column="1"
        android:layout_weight="0.63"
        android:height="140dp"
        android:background="@drawable/custom_button_scanner" />


</TableRow>

And I have a Activity in this way:

public class ActivityListview extends AppCompatActivity {

    //************VARIABILI GLOBALI*************

    ListView lv;

    static final String KEY_CF = "CodiceFiscale";
    static final String KEY_ID = "IDSoggetto";
    static final String KEY_NOMEC = "NomeCompleto";
    static final String KEY_TIPOSOGGET = "TipoSoggettoEtichetta";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_scanner);


        lv = (ListView) findViewById(R.id.listView_resultXML);


       Example();

    }



    public void Example() {
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[]{KEY_CF, KEY_ID, KEY_NOMEC, KEY_TIPOSOGGET}, new int[]{
                R.id.cf, R.id.idsogg, R.id.nomec, R.id.tiposogget});
        // selecting single ListView item
        ViewGroup headerview = (ViewGroup) getLayoutInflater().inflate(R.layout.header_listview, lv, false);
        lv.setAdapter(null);
        lv.addHeaderView(headerview);
        lv.setAdapter(adapter);


        // HERE I WOULD LIKE TO SET ONCLICK BUTTON



        // listening to single listitem click
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            //Metodo per prelevare dati al click sulla casella della ListView
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String st_cf = ((TextView) view.findViewById(R.id.cf)).getText().toString();
                //String st_cognome = ((TextView) view.findViewById(R.id.cognome)).getText().toString();
                //String st_eliminato = ((TextView) view.findViewById(R.id.eliminato)).getText().toString();
                // String st_esterno = ((TextView) view.findViewById(R.id.esterno)).getText().toString();
                String st_idsogg = ((TextView) view.findViewById(R.id.idsogg)).getText().toString();
                //String st_nome = ((TextView) view.findViewById(R.id.nome)).getText().toString();
                String st_nomec = ((TextView) view.findViewById(R.id.nomec)).getText().toString();
                //String st_tiposogg = ((TextView) view.findViewById(R.id.tiposogg)).getText().toString();
                String st_tiposogget = ((TextView) view.findViewById(R.id.tiposogget)).getText().toString();
                Toast.makeText(ActivityListview.this, "Example onclick", Toast.LENGTH_LONG).show();

            }
        });
    }
}

How to set onClick event for Button in list item in Activity?

6
  • 7
    stackoverflow.com/questions/12596199/… Commented May 13, 2016 at 10:09
  • write in your custom adapter class Commented May 13, 2016 at 10:09
  • Implement inside getview of customAdapter Commented May 13, 2016 at 10:12
  • Can you post an example please? =) Commented May 13, 2016 at 10:13
  • Can you post SimpleAdapter code? for more understanding Commented May 13, 2016 at 10:15

2 Answers 2

0
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.puzzles_row, null);
        }
        ListedPuzzle lp = items.get(position);
        if (lp != null) {
            TextView title = (TextView) v.findViewById(R.id.listTitles);
            //set as the tag the position parameter 
            title.setTag(new Integer(position));                    
            title.setOnclickListener(new OnCLickListener(){

            @Override 
            public void onClick(View v) {
                // Do the stuff you want for the case when the row TextView is clicked
                // you may want to set as the tag for the TextView the position paremeter of the `getView` method and then retrieve it here
                Integer realPosition = (Integer) v.getTag();
                // using realPosition , now you know the row where this     TextView was clicked
            }
        }); 
            title.setText(lp.getTitle());
            CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
            star.setChecked(lp.isStarred());
        }
        return v;
    }

Take above example as modify it

Sign up to request clarification or add additional context in comments.

4 Comments

For use this method in class that extends Activity?
No SimpleAdapter class will have method getView check it
and how use it in Activity?
Either you implement within Adapter or you can use Callback interfaces
0

One possibility: You could set the listener in your custom adapter, then create an interface you call when the button is clicked.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.