Question

I want to make a custom ListView with buttons as Items. Now I'm using the OnItemClickListener and for each click, a toast message comes up, but it only comes up, if i press out of the buttons, but in the ListView(see picture)

enter image description here

How can I do it, that the toast comes, if i press on the buttons, not out of them.

Adaptercode:

public class MainListAdapter extends ArrayAdapter<Games> {

Context context;
int layoutResourceId;
Games data[] = null;

public MainListAdapter(Context context, int layoutResourceId, Games[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View row = convertView;
    ListHolder holder = null;

    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ListHolder();
        holder.btn = (Button)row.findViewById(R.id.listViewButton);

        row.setTag(holder);
    }else{
        holder = (ListHolder)row.getTag();
    }
    Games games = data[position];
    holder.btn.setText(games.name);

    return row;
}

static class ListHolder {
    Button btn;
}

}

In the Acitivty:

MainListAdapter adapter = new MainListAdapter(this, R.layout.listview_item_row, games_data);

    listView1 = (ListView)findViewById(R.id.listView1);

    final View header = (View)getLayoutInflater().inflate(R.layout.listview_newgame_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);

    listView1.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_LONG).show();
        }

    });

listview_item_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/listViewButton"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:focusable="false"
    android:background="@drawable/button" />

Was it helpful?

Solution

IN your Adupter class make two objects for button.

And setonclicklistner for the buttons in adupter view

E.g.

        vi = inflater.inflate(R.layout.list_row_contact, null);


    LinearLayout ll_adpt_main =   (LinearLayout)vi.findViewById(R.id.ll_adpt_main);
    TextView name = (TextView) vi.findViewById(R.id.name);
    TextView discription = (TextView) vi.findViewById(R.id.number);
    ImageView iv = (ImageView)vi.findViewById(R.id.iv);





    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });





    return vi;
}

OTHER TIPS

Make a separate onclicklistener for buttons. OnItemclicklistener is for a list item.

Firstly you have to create an xml layout like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Then create an adapter class:

class MyAdapter extends ArrayAdapter<View.OnClickListener> {
    public MyAdapter(Context context, int textViewResourceId, View.OnClickListener[] objects) {
        super(context, textViewResourceId, objects);
    }
    public MyAdapter(Context context, int resource, int textViewResourceId, List<View.OnClickListener> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.event_layout, null);

        Button b = (Button) convertView.findViewById(R.id.btn);

        View.OnClickListener l = getItem(position);
        b.setOnClickListener(l);

        return convertView;
    }
}

The layout of the activity should be like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LayoutActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".YourActivityName" >
        <Button
            android:id="@+id/btnHeader1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
        <Button
            android:id="@+id/btnHeader2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

       <ListView
           android:id="@+id/your_list"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
       </ListView>
</LinearLayout>

Now to set list items:

ListView list = (ListView)findViewById(R.id.your_list);

View.OnClickListener[] listeners = ....;

MyAdapter adapter = new MyAdapter(context, R.layout.your_list_layout_adapter, listeners);
list.setAdapter(adapter);

I hope to have satisfied all your requests :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top