自定义数组适配器,getview()没有被调用

时间:2015-11-26 16:44:21

标签: android

这将搜索所有可用的WiFi网络,然后将它们返回到字符串数组wifiList。我之后尝试做的是在我的自定义列表视图中显示数组值。如果我使用内置的普通基本列表视图,我会得到结果,所以我知道它正在工作。但我似乎无法弄清楚为什么getview没有被调用因此我的列表视图没有被填充。

public class WifiListActivity extends ListActivity {

ListView lv;
WifiManager wifiManager;
String wifiList[];
WifiScanReceiver wifiReciever;
WifiConfiguration config = new WifiConfiguration();
Boolean SCAN_COM = false;
Boolean List_Set = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi_list);


    // connect lv listView variable with the list view in XML
    lv = (ListView) findViewById(android.R.id.list);
    // initialize WifiManager
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    // initialize a wifiReceiver object
    wifiReciever = new WifiScanReceiver();
    // start scanning
    wifiManager.startScan();

    //Connect();


}

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

protected void onResume() {
    registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    super.onResume();
}

/*
 * Connect to selected wifi device
 */
public void Connect() {
    // do something
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String test = (String) parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(), " " + test + " " + id, Toast.LENGTH_SHORT).show();

            //Algorithm to connect to wireless network
            config.SSID = String.format("\"%s\"", test);
            config.preSharedKey = null;
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            int netID = wifiManager.addNetwork(config);
            wifiManager.disconnect();
            wifiManager.enableNetwork(netID, true);
            wifiManager.reconnect();
        }
    });
}


/*
 * To get a list of SSIDs from all the wifi information
 */
public String[] getSSID(List<ScanResult> wifiScanList) {
    //Loops through list and stores wifi information into string "wifis"
    for (int i = 0; i < wifiScanList.size(); i++) {
        //store the wifi information into the variable wifiInfo
        String wifiInfo = wifiScanList.get(i).toString();
        // declare a char arayList to store ssid
        ArrayList<Character> temp = new ArrayList<Character>();
        int j = 0;
        int k = 0;

        // loop through wifiInfo and store the ssid as a char arrayList
        while (wifiInfo.charAt(j) != ',') {
            //check each character in wifiInfo
            temp.add(wifiInfo.charAt(j));
            j++;
        }
        //convert char arrayList to string
        String ssid = getString(temp);
        //get the substring of temp arrayList to just display the name of network
        ssid = ssid.substring(6, ssid.length());
        //store ssid into the wifis list
        wifiList[i] = ssid;
    }

    // create a String array to store the wifi list that has duplicates removed
    String[] filteredWifiList = RemoveDuplicates(wifiList);

    return filteredWifiList;
}

/*
 * Remove duplicated SSIDs from Wifi list
 */
public String[] RemoveDuplicates(String[] wifiList) {
    // create a temporary ArrayList to store the filtered elements
    List<String> tempArray = new ArrayList<String>();
    // loop through the wifiList which contains several duplicated elements
    // extract only one for each name and store into tempArray
    for (int i = 0; i < wifiList.length; i++) {
        if (!tempArray.contains(wifiList[i])) {
            tempArray.add(wifiList[i]);
        }
    }
    // convert the tempArray ArrayList to a String array
    String[] filteredArray = new String[tempArray.size()];
    filteredArray = tempArray.toArray(filteredArray);

    return filteredArray;
}

/*
 * Convert character list to a String
 */
public String getString(ArrayList<Character> list) {
    StringBuilder builder = new StringBuilder(list.size());
    for (Character ch : list) {
        builder.append(ch);
    }
    return builder.toString();
}

private class WifiScanReceiver extends BroadcastReceiver {
    public void onReceive(Context c, Intent intent) {
        //Creates a list that stores wifi information
        List<ScanResult> wifiScanList = wifiManager.getScanResults();
        //Defines size of the list
        wifiList = new String[wifiScanList.size()];

        //get the SSID from the wifi information
        wifiList = getSSID(wifiScanList);
        //lv.setAdapter(new MyAdapter());
        //Displays list
        //lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, wifiList));
        lv.setAdapter(new MyAdapter());
        //setListAdapter(new MyAdapter());

    }


}

private class MyAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return wifiList.length;
    }

    @Override
    public String getItem(int position) {
        return wifiList[position];
    }

    @Override
    public long getItemId(int position) {
        return wifiList[position].hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.activity_wifi_list_detail, container, false);
        }

        ((TextView) convertView.findViewById(R.id.flareName))
                .setText(getItem(position));
        return convertView;
    }
}

}

0 个答案:

没有答案