如何在不使用自定义适配器的情况下将子项添加到列表视图中

时间:2014-09-19 06:00:49

标签: android listview android-listview subitem

在我的listview中,我得到了一个wi-fi的可用列表,其中包含一些数据我想要的是将数组项[0]显示为项目和数组项[2]作为子项而不使用自定义适配器和消耗品列表视图

以下是我将数据添加到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));


              }
           }
}

1 个答案:

答案 0 :(得分:1)

您只需使用自定义适配器即可实现此目的。这是我从我为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();
    }

}

你应该从你的MainActivity中调用它,如下所示:

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");

你的listview_subitem.xml也可以这样:

<?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>

,你的listview_item.xml可以是:

<?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>

你的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>