Search code examples
androidlistviewandroid-listviewsubitem

How to add a subitem to a listview without using custom adapter


In my listview i am gettinng a list of available of wi-fi with some data what i want is to display the array item[0] as a item and array item[2] as a subitem without using custom adapter and expendable listview

Here is the code in which i am adding data into listview.

import java.util.List;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

public class MainActivity extends Activity {
       WifiManager mainWifiObj;
       WifiScanReceiver wifiReciever;
       ExpandableListView list;
       String wifis[];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ExpandableListView)findViewById(R.id.ListView1);
          mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);
          wifiReciever = new WifiScanReceiver();
          mainWifiObj.startScan();

    }
       protected void onPause() {
              unregisterReceiver(wifiReciever);
              super.onPause();
           }

       protected void onResume() {
              registerReceiver(wifiReciever, new IntentFilter(
              WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
              super.onResume();
           }
       class WifiScanReceiver extends BroadcastReceiver {
              @SuppressLint("UseValueOf")
              public void onReceive(Context c, Intent intent) {
                 List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
                 wifis = new String[wifiScanList.size()];
                 for(int i = 0; i < wifiScanList.size(); i++){
                    wifis[i] = ((wifiScanList.get(i)).toString());
                 }
                 String filtered[] = new String[wifiScanList.size()];
                    int counter = 0;
                    for (String eachWifi : wifis) {
                        String[] temp = eachWifi.split(",");

                        filtered[counter] = temp[0].substring(5).trim()+"\n" + temp[2].substring(12).trim()+"\n" +temp[3].substring(6).trim();//0->SSID, 2->Key Management 3-> Strength


                        counter++;

                    }
                    list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                            android.R.layout.simple_list_item_1, filtered));


              }
           }
}

Solution

  • You can achive this by only using a custom adapter. Here is an example that I converted for your problem from a custom adapter that I created for my listview:

    import java.util.ArrayList;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class CustomListAdapter extends BaseAdapter {
    
        private static final int TYPE_ITEM = 0;
    
        private static final int TYPE_SUBITEM = 1;
    
        private static final int TYPE_MAX_COUNT = TYPE_SUBITEM  + 1;
    
        private ArrayList<String> listViewItems;
    
        public LayoutInflater mInflater;
    
        public CustomListAdapter(Activity act){
            this.listViewItems= new listViewItems<String>();
            mInflater = (LayoutInflater) act.getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        }
    
        @SuppressLint("InflateParams")
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            //this finds out the type of the view that is being created
            int type = getItemViewType(position); 
    
            switch (type) {
                case TYPE_ITEM:
                    if(convertView == null)
                        convertView = mInflater.inflate(R.layout.listview_item, null);
                    //the following is an example to change things in subitem 
                    TextView text_of_item = (TextView) convertView.findViewById(R.id.textView_item);
                    text_of_item.setText("item"+position);
                    break;
    
                case TYPE_SUBITEM:
                    if(convertView == null)
                        convertView = mInflater.inflate(R.layout.listview_subitem, null);
                    //the following is an example to change things in subitem 
                    TextView text_of_subitem = (TextView) convertView.findViewById(R.id.textView_subitem);
                    text_of_subitem.setText("subitem"+position);
                    break;
            }
    
            return convertView;
        }
    
        @Override
        public int getItemViewType(int position) {
               //you can achive this part via using integer IDs. Just change your ArrayList<String> to ArrayList<int>
    
            if(listViewItems.get(position).equals("item")){//I suppose you give unique names to your subitems
                return TYPE_ITEM;
            }else if(listViewItems.get(position).equals("subitem")){//I suppose you give unique names to your subitems
                return TYPE_SUBITEM;
        }
    
        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }
    
    
        @Override
        public int getCount() {
            return listViewItems.size();
        }
    
        @Override
        public String getItem(int position) {       
            return listViewItems.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public void addItem(String item){
            listViewItems.add(item);
            notifyDataSetChanged();
        }
    
        public void removeItem(int position){
            listViewItems.remove(position);
            notifyDataSetChanged();
        }
    
    }
    

    and you should call this from your MainActivity as follows:

    CustomListAdapter  mCustomListAdapter = new CustomListAdapter (this);
    
    ListView mList = (ListView) findViewById(R.id.listview);
    mList.setAdapter(mCustomListAdapter);
    
    //when you want to add item to your listview just add the following:
    mCustomListAdapter.addItem("item");
    
    //when you want to add subitem to your listview just add the following:
    mCustomListAdapter.addItem("subitem");
    

    also your listview_subitem.xml can be like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list_subitem"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
    
        <TextView
        android:id="@+id/textView_subitem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    
    </LinearLayout>
    

    and your listview_item.xml can be this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal" >
    
        <TextView
        android:id="@+id/textView_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    </LinearLayout>
    

    your activity_main.xml:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- Listview to display slider menu -->
        <ListView
            android:id="@+id/listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>