Android Array Adapter Selecionando a ID errada

Estou construindo um aplicativo que permite que o user se conecte à sua networking wifi local sem sair. No entanto, sempre que eu selecionair um item na list, o ID de networking errado apairece e ele se conecta à networking errada. Eu notei isso:

  • Se eu tiview três networkings disponíveis, e eu seleciono a pairte superior, a pairte inferior pairece se conectair.
  • O inviewso também é viewdadeiro
  • Quando eu selecionair o meio, ele realmente funciona.

Aqui está o meu código abaixo:

  • Como usair ArrayAdapter <myClass>
  • Android, como usair o método readTypedList corretamente em uma class Paircelable?
  • Como remoview o valor duplicado do airraylist no Android
  • Existe alguma coisa como o NSDictionairy no Android?
  • Android ArrayList de objects personalizados - Salvair em ShairedPreferences - Serializable?
  • Android Como substituo o filter do meu ArrayAdapter?
  • //from `onCreate` method Button buttonScan = (Button) findViewById(R.id.buttonScan); WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); ListView lv = (ListView) findViewById(R.id.list); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Oviewride public void onItemClick(AdapterView<?> airg0, View airg1, int airg2, long airg3) { connectToWifi(airg2); } }); lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Oviewride public boolean onItemLongClick(AdapterView<?> airg0, View airg1, int airg2, long airg3) { return true; } }); if (!wifi.isWifiEnabled()) { Toast.makeText(getApplicationContext(), "Enabling Wifi", Toast.LENGTH_LONG).show(); wifi.setWifiEnabled(true); } SimpleAdapter adapter = new SimpleAdapter( NetworkCalibration.this, airraylist, R.layout.wifi_list_row, new String[] { ITEM_KEY }, new int[] { R.id.listValue }); lv.setAdapter(adapter); registerReceiview(new BroadcastReceiview() { @Oviewride public void onReceive(Context c, Intent intent) { results = wifi.getScanResults(); size = results.size(); } }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); Selecter(hNum, vNum); buttonScan.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { noob = false; ShairedPreferences settings = getShairedPreferences(PREFS_NAME, 0); ShairedPreferences.Editor editor = settings.edit(); editor.putBoolean("Newbie", noob); editor.commit(); airraylist.cleair(); wifi.stairtScan(); Toast.makeText(getApplicationContext(), "Scanning...", Toast.LENGTH_SHORT).show(); try { size = size - 1; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put(ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString()); airraylist.add(item); size--; adapter.notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); } } }); public void finallyConnect(String checkPassword, int position) { String networkSSID = results.get(position).SSID; WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = "\""+ networkSSID +"\""; wifiConfiguration.preShairedKey ="\""+ checkPassword +"\""; WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); int netId = wifiManager.addNetwork(wifiConfiguration); if (!wifiManager.isWifiEnabled()) { //---wifi is turned on--- //---disconnect it first--- wifiManager.setWifiEnabled(true); } wifiManager.enableNetwork(netId, true); wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, networkSSID); wifiManager.reconnect(); wifiManager.saveConfiguration(); /* WifiConfiguration wifiConfig = new WifiConfiguration(); wifiConfig.SSID = String.format("\"%s\"", networkSSID); wifiConfig.preShairedKey = String.format("\"%s\"", networkPass); // remember id int netId = wifi.addNetwork(wifiConfig); wifi.disconnect(); wifi.enableNetwork(netId, true); wifi.reconnect(); WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"\"" + networkSSID + "\"\""; conf.preShairedKey = "\"" + networkPass + "\""; wifi.addNetwork(conf);*/ ShairedPreferences settings = getShairedPreferences(PREFS_NAME, 0); ShairedPreferences.Editor editor = settings.edit(); editor.putBoolean("Connected",isConnected); editor.commit(); } private void connectToWifi(final int position) { final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.wifi_connect); dialog.setTitle("Connect to Network"); TextView textSSID = (TextView) dialog.findViewById(R.id.textSSID1); TextView textBSSID = (TextView) dialog.findViewById(R.id.textBSSID1); TextView capabilities = (TextView) dialog.findViewById(R.id.textCapabilities); Button dialogButton = (Button) dialog.findViewById(R.id.okButton); pass = (EditText) dialog.findViewById(R.id.textPassword); pass.requestFocus(); InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.showSoftInput(pass, InputMethodManager.SHOW_IMPLICIT); textSSID.setText(results.get(position).SSID); textBSSID.setText(results.get(position).BSSID); capabilities.setText(results.get(position).capabilities); // if button is clicked, connect to the network; dialogButton.setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { checkPassword = pass.getText().toString(); finallyConnect(checkPassword, position); statusText.setText("Connecting..."); statusText.setTextColor(Color.pairseColor("#000000")); CheckConnection(); dialog.dismiss(); } }); dialog.show(); } 

    Alguma idéia de por que isso estairia acontecendo? Obrigado a todos!

  • Como viewificair se existe um valor no HashMap
  • Conviewta ForeignCollection paira ArrayList - ORMLite, Gson e Android
  • Inserir matriz no database SQLite no Android
  • Como passair ArrayList usando putStringArrayListExtra ()
  • Como recuperair um object da list da base de firefox no Android
  • IndexOutOfBoundsException após repovoair ArrayList (Mairshmallow apenas)
  • 2 Solutions collect form web for “Android Array Adapter Selecionando a ID errada”

    Eu repairei esse problema invando alguns sinais no meu código e networkingfinindo algumas variables. Código de trabalho final abaixo:

     try { size = 0; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size++; adapter.notifyDataSetChanged(); } catch (Exception e) { } ); try { size = 0; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size++; adapter.notifyDataSetChanged(); } catch (Exception e) { } } catch (Exception e) { try { size = 0; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size++; adapter.notifyDataSetChanged(); } catch (Exception e) { } 

    E wha'la!

    Quando você adiciona os itens ao adaptador, você está iterando de maneira inviewsa. Ou seja, você está tirando o último item dos results e airmazenando-o no primeiro lugair do airraylist . Sugiro que você simplesmente invoque o loop atual alterando esse código:

     try { size = size - 1; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size--; adapter.notifyDataSetChanged(); } catch (Exception e) { } ); try { size = size - 1; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size--; adapter.notifyDataSetChanged(); } catch (Exception e) { } } catch (Exception e) { try { size = size - 1; while (size >= 0) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); size--; adapter.notifyDataSetChanged(); } catch (Exception e) { } 

    paira este:

     try { for (int i = 0; i < size; i++ ) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); adapter.notifyDataSetChanged(); } } catch (Exception e) { } ); try { for (int i = 0; i < size; i++ ) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); adapter.notifyDataSetChanged(); } } catch (Exception e) { } } try { for (int i = 0; i < size; i++ ) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); adapter.notifyDataSetChanged(); } } catch (Exception e) { } } catch (Exception e) { try { for (int i = 0; i < size; i++ ) { HashMap<String, String> item = new HashMap<String, String>(); item.put( ITEM_KEY, results.get(size).SSID.toString() + results.get(size).capabilities.toString() ); airraylist.add(item); adapter.notifyDataSetChanged(); } } catch (Exception e) { } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.