Android - Listview项目位置下降

时间:2015-07-27 21:15:06

标签: android listview

我正在构建一个应用程序,它可以接收我家周围的所有无线网络。

列表视图中列出了所有无线网络。

现在我想在点击某个listview项目时打开一个新活动。但在此之前,我想从所选项目中获取SSID。

我正在使用onItemClick从项目获取位置并且工作正常。我得到的问题是,当我点击某个项目时,我从当前所选项目获得了不同的SSID,这些值正在被显示:

WIFI 1 - When I click here I get the SSID from WIFI 3

WIFI 2 - When I click here I get the SSID from WIFI 2

WIFI 3 - When I click here I get the SSID from WIFI 1

当我点击wifi等等时,而不是从wifi 1获取SSID

你们可以检查一下我做错了什么吗?

我的代码:

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textStatus = (TextView) findViewById(R.id.textView1);
        buttonScan = (Button) findViewById(R.id.button1);

        lv = (ListView)findViewById(R.id.text);

        lv.setClickable(true);

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
            wifi.setWifiEnabled(true);
        }   
        this.adapter = new SimpleAdapter(MainActivity.this, arraylist, android.R.layout.simple_list_item_1, new String[] { ITEM_KEY }, new int[] { android.R.id.text1 });
        lv.setAdapter(this.adapter);

        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
               results = wifi.getScanResults();
               size = results.size();


            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));       


    }

    @Override
    protected void onResume() {
        super.onResume();
        final Handler handler = new Handler();

        Runnable refresh = new Runnable() {
                @Override
                public void run() {
                   test();
                    handler.postDelayed(this, 8000);
                }
            };

        handler.postDelayed(refresh, 2000);   
    }



    private void test() {
    {

        arraylist.clear();          
        wifi.startScan();

        Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
        try 
        {
            size = size - 1; 
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                    // TODO Auto-generated method stub
                    Log.e("", "" + results.get(arg2).SSID);
                }

            });

            while (size >= 0) 
            {  
                LinkedHashMap<String, String> item = new LinkedHashMap<String, String>(); 

                String BSSID = results.get(size).BSSID;

                int frequency = results.get(size).frequency;

                if(!results.get(size).SSID.equals("FET")) { 

                item.put(ITEM_KEY, results.get(size).SSID.concat("")); 
                arraylist.add(item); 

                }

                size--;
                adapter.notifyDataSetChanged();  


            } 

        }
        catch (Exception e) 
        { }         

    }    
}


}

1 个答案:

答案 0 :(得分:0)

撤消将列表项添加到arraylist

的顺序
try 
{
    size = 0;
    ...
    while (size < results.size())
    {
      ...
      arraylist.add(item);
      ...
      size++;
      ...
    }
}