从ListView android动态删除项目

时间:2015-03-27 10:42:37

标签: android listview android-listview intentfilter

我的listview会保存我所在大学的所有接入点。我想只显示名为eduroam-setup的点访问。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="projectone.WiFiActivity">

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/toggleButton"
        android:textOn="Wi-Fi On"
        android:textOff="Wi-Fi Off"
        android:onClick="onToggleClicked"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:checked="false" />


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/toggleButton"
        android:textFilterEnabled="false" />
</RelativeLayout>

这是我的.java

public class WiFiActivity extends ActionBarActivity {

    private WifiManager wifiManager;
    private BroadcastReceiver wifiReciever;
    private ArrayAdapter adapter;


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

        ListView listview = (ListView) findViewById(R.id.listView);

        adapter = new ArrayAdapter
                (this,android.R.layout.simple_list_item_1);

        listview.setAdapter(adapter);

        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        wifiReciever = new WiFiScanReceiver();

    }

    public void onToggleClicked(View view) {

        adapter.clear();

        ToggleButton toggleButton = (ToggleButton) view;

        if (wifiManager == null) {
            // Device does not support Wi-Fi
            Toast.makeText(getApplicationContext(), "Oop! Your device does not support Wi-Fi",
                    Toast.LENGTH_SHORT).show();
            toggleButton.setChecked(false);

        } else {
            if (toggleButton.isChecked()) { // To turn on Wi-Fi
                if (!wifiManager.isWifiEnabled()) {

                    Toast.makeText(getApplicationContext(), "Wi-Fi is turned on." +
                                    "\n" + "Scanning for access points...",
                            Toast.LENGTH_SHORT).show();

                    wifiManager.setWifiEnabled(true);

                } else {
                    Toast.makeText(getApplicationContext(), "Wi-Fi is already turned on." +
                                    "\n" + "Scanning for access points...",
                            Toast.LENGTH_SHORT).show();
                }
                wifiManager.startScan();

            } else { // To turn off Wi-Fi
                Toast.makeText(getApplicationContext(), "Wi-Fi is turned off.",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
    class WiFiScanReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                List<ScanResult> wifiScanResultList = wifiManager.getScanResults();
                for (int i = 0; i < wifiScanResultList.size(); i++) {
                    ScanResult accessPoint = wifiScanResultList.get(i);
                    String listItem = " Name: " + accessPoint.SSID + " || Mac: " + accessPoint.BSSID + " || Signal Strenght: " + accessPoint.level+ "dBm";
                    adapter.add(listItem);
                }
            }
        }
    }


    protected void onResume() {
        super.onResume();
        // Register the BroadcastReceiver for SCAN_RESULTS_AVAILABLE_ACTION
        IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(wifiReciever, filter);
    }
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(wifiReciever);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

到目前为止,我的列表视图显示了所有网络。我想过滤列表只显示eduroam-setup。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在您的功能中,只需根据需要进行过滤:

public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(action)) {
                List<ScanResult> wifiScanResultList = wifiManager.getScanResults();
                for (int i = 0; i < wifiScanResultList.size(); i++) {
                    ScanResult accessPoint = wifiScanResultList.get(i);
                    String listItem = " Name: " + accessPoint.SSID + " || Mac: " + accessPoint.BSSID + " || Signal Strenght: " + accessPoint.level+ "dBm";
                    if (listItem.contains("eduroam-setup")) {
                        //This will only be called if the network
                        //has "eduroam-setup" in the name.
                        //If it does, add it to your list
                        adapter.add(listItem);
                    }
                }
            }
        }