Skip to content Skip to sidebar Skip to footer

How To Display Checkbox On Every Entry In A Listview

From this Activity i get text from textField and display it in a ListView. Now i want to to add check box on every entry in a listView Cell and also like to know how to display mor

Solution 1:

You have to use a BaseAdapter and some Getter/Setter methods to add multiple texts/images/other UI elements in each item of your list view.

You have to implement multiple things to get this result. Here they are --

  1. Create a Custom Layout for each item of your ListView.

listview_item_layout.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/layout_textview1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15sp"android:layout_marginRight="5dip"android:textStyle="bold"/><TextViewandroid:id="@+id/layout_textview2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15sp"android:layout_marginLeft="5dip"android:textStyle="bold"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/checkbox"android:text="Test"/></LinearLayout>
  1. Create a custom class and add some Getter/Setter methods.

ListRowItem.java

publicclassListRowItemimplementsSerializable{
String carrier,number;

publicStringgetCarrier(){
    return carrier;
}

publicStringgetNumber(){
    returnnumber;
}

publicvoidsetCarrier(String ba_carrier){
    carrier = ba_carrier;
}

publicvoidsetNumber(String ba_number){
    number = ba_number;
}
}
  1. Create a custom class and extend the BaseAdapter class.

    publicclassMyBaseAdapterextendsBaseAdapter {
    
    public Context ba_context;
    public ArrayList<ListRowItem> listitem = newArrayList<>();
    public LayoutInflater inflater;
    ListRowItem currentlistitem;
    
    publicMyBaseAdapter(Context ma_context, ArrayList<ListRowItem> ma_listitem) {
    super();
    this.ba_context = ma_context;
    this.listitem = ma_listitem;
    
    inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @OverridepublicintgetCount() {
    returnthis.listitem.size();
    }
    
    @Overridepublic Object getItem(int position) {
    returnthis.listitem.get(position);
    }
    
    @OverridepubliclonggetItemId(int position) {
    return (long) position;
    }
    
    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    Viewvi= convertView;
    
    if (convertView == null)
        vi = inflater.inflate(R.layout.listview_item_layout, parent, false);
    
    TextViewcarrier= (TextView) vi.findViewById(R.id.layout_textview1);
    TextViewnumber= (TextView) vi.findViewById(R.id.layout_textview2);
    
    currentlistitem = listitem.get(position);
    
    Stringstr_carrier= currentlistitem.getCarrier();
    Stringstr_number= currentlistitem.getNumber();
    
    carrier.setText(str_carrier);
    number.setText(str_number);
    
    return vi;
    }
    }
    
  2. Finally, populate your ArrayList and set the Adapter in your MainActivity.

    ArrayList<ListRowItem> listitem = newArrayList<>();
    Contextcontext= TestActivity.this;
    MyBaseAdapter baseAdapter;
    
    ListRowItemlr=newListRowItem();
    lr.setNumber(number);
    lr.setCarrier(carrier);
    
    listitem.add(lr);
    
    baseAdapter = newMyBaseAdapter(context,listitem);
    
    setContentView(R.layout.activity_test);
    listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(baseAdapter);
    

Hope this helps!!

Post a Comment for "How To Display Checkbox On Every Entry In A Listview"