如何在列表视图中获取所有项目?

时间:2019-05-23 09:14:45

标签: android listview

我实现了一个代码来扫描wifi网络并将其显示在列表中。从列表中选择一个wifi网络后,我在另一个ListView的另一个活动中添加了选定的网络。 现在,当我重新启动应用程序并再次扫描wifi网络时,我也获得了之前选择并添加到列表中的那个网络。 (在一项活动中,我搜索可用的wifi网络,而在另一项活动中,我添加了该选定网络的列表) 所以我的问题是,如何隐藏已在列表视图中添加的网络?

这是我的活动,在这里我已在列表视图中显示添加的设备

@Override     受保护的void onCreate(捆绑的savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);

    btn = (Button)findViewById(R.id.btnAdd);
    btn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Intent intent = new Intent(MainActivity.this,Second.class);
            startActivity(intent);
            finish();
        }
    });



    try
    {
        listView          = (ListView)findViewById(R.id.device_list);
        devicesNameEspNum = new ArrayList<>();
        devices           = new Devices();
        devicesList       = alldeviceList();

        for(int i=0; i<devicesList.size(); i++)
        {
            devices = devicesList.get(i);
            devicesNameEspNum.add(devices);
        }

        devicesAdapter = new DevicesAdapter(MainActivity.this,devicesNameEspNum);
        listView.setAdapter(devicesAdapter);
        devicesAdapter.notifyDataSetChanged();
    }
    catch (NullPointerException ex)
    {
        ex.printStackTrace();
    }



    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id)
        {
            Intent intent   = new Intent(MainActivity.this,Fourth.class);
            String selected = ((TextView) view.findViewById(R.id.deviceNamList)).getText().toString();
            intent.putExtra("DEVICE_ID",selected);
            startActivity(intent);
            finish();
        }
    });

    final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
    {
        Toast.makeText(this, "****", Toast.LENGTH_SHORT).show();
    }
}

public static  List<Devices> alldeviceList()
{
    return new Select().from(Devices.class).execute();
}

这是我第二次扫描附近的wifi网络的活动  @Override     受保护的void onCreate(捆绑的savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_second);

    list=getListView();
    mainWifiObj = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    if(mainWifiObj != null)
    {
        mainWifiObj.setWifiEnabled(true);
    }

    wifiReciever = new WifiScanReceiver();
    mainWifiObj.startScan();


    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) !=  PackageManager.PERMISSION_GRANTED)
    {
        requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1000);

    }
    else
    {
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    }


    // listening to single list item on click
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // selected item

            String ssid = ((TextView) view).getText().toString();
            connectToWifi(ssid);
            Toast.makeText(Second.this,"Wifi SSID : "+ssid,Toast.LENGTH_SHORT).show();

        }
    });


}



class WifiScanReceiver extends BroadcastReceiver
{
    @SuppressLint("UseValueOf")
    public void onReceive(Context c, Intent intent)
    {
        List<ScanResult> wifiScanList = mainWifiObj.getScanResults();

        System.out.println("Wifi Scan Result: "+wifiScanList);

        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++;

        }

        System.out.println("Filtered :"+filtered);
        list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,R.id.label, filtered));


    }
}

1 个答案:

答案 0 :(得分:0)

这是什么?

displayed.addAll(allItems.filter { !it.equals("abc") })