[Solved] How can i add two buttons to the listview from the adapter?


For ListViews, there are two layout files used. One specifies the layout for the list as a whole, the other specifies the layout for the list_item.

List Item Layout:
list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:baselineAligned="false"
        android:orientation="horizontal">

    <TextView
        android:id="@+id/contact_phone"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="0.73"
        android:background="#00000000"
        android:gravity="start|center_vertical"
        android:text=""
        android:textColor="#FFFFFFFF"
        android:textSize="16sp" 
        />

    <Button
        android:id="@+id/do_phone"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="@string/call"
        android:textSize="16sp"
        android:textColor="#FFFFFFFF"
        android:background="@android:color/holo_red_dark"
        />
   <Button
        android:id="@+id/do_map"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="@string/map"
        android:textSize="16sp"
        android:textColor="#FFFFFFFF"
        android:background="@android:color/holo_red_dark"
        />
    </LinearLayout>

Then, the applicable code in the adapter is this:

ListAdapter.java

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
    LayoutInflater mInflater = (LayoutInflater)
            context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    convertView = mInflater.inflate(R.layout.list_item, null);
}

Button btnCall = (Button) convertView.findViewById(R.id.do_phone);
btnCall.setTag(position);
btnCall.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Integer position = (Integer)v.getTag();
        // This calls a method back in the fragment or activity that owns the adapter
        fragment.callItemList(position);
    }
});

Button btnMap = (Button) convertView.findViewById(R.id.do_map);
btnMap.setTag(position);
btnMap.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Integer position = (Integer)v.getTag();
        // This calls a method back in the fragment or activity that owns the adapter
        fragment.mapItemList(position);
    }
});

TextView txtContactPhone = (TextView) convertView.findViewById(R.id.contact_phone); 

return convertView;
}

3

solved How can i add two buttons to the listview from the adapter?