Fill listview

0

I am trying to fill listview from arralylist but it generates an error and closes the application:

devices.get(0)=Cannot find local variable 'devices'

If I comment the line listView.setAdapter(adapter); and do a debug in arrayListDevice.add(device.getName()); I can see how the values are stored without problems the data in arraylist .

And this is what I have so far:

package com.traceroute.kitkat;

import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import io.particle.android.sdk.cloud.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;

public class DeviceSelection extends AppCompatActivity {

    ListView listView;

    int cantidad=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_device_selection);
        listView = findViewById(R.id.lvDeviceList);


        final ArrayList<String> arrayListDevice = new ArrayList<>();

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



        Thread t = new Thread(new Runnable() {

            @Override

            public void run() {

                try {

                    List<ParticleDevice> devices = ParticleCloudSDK.getCloud().getDevices();

                    for (ParticleDevice device : devices) {


                        arrayListDevice.add(device.getName());


                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {


                            Toast.makeText(getApplicationContext(), "Dispositivos...", Toast.LENGTH_SHORT).show();
                        }
                    });
 listView.setAdapter(adapter);


                } catch (ParticleCloudException e) {
                    e.getBestMessage();
                }

            }
        });

        t.start();
    }
}
    
asked by Mike Sum 06.06.2018 в 19:15
source

1 answer

0

now I get this error

E / AndroidRuntime: FATAL EXCEPTION: Thread-6                   Process: com.traceroute.kitkat, PID: 26922                   android.view.ViewRootImpl $ CalledFromWrongThreadException: Only the original thread that created a view of the hierarchy can touch its views.                       at android.view.ViewRootImpl.checkThread (ViewRootImpl.java:7406)                       at android.view.ViewRootImpl.focusableViewAvailable (ViewRootImpl.java:3473)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.ViewGroup.focusableViewAvailable (ViewGroup.java:848)                       at android.view.View.setFlags (View.java:13352)                       at android.view.View.setFocusableInTouchMode (View.java:9531)                       at android.widget.AdapterView.checkFocus (AdapterView.java:752)                       at android.widget.ListView.setAdapter (ListView.java:576)                       at com.traceroute.kitkat.DeviceSelection $ 1.run (DeviceSelection.java:84)                       at java.lang.Thread.run (Thread.java:764) E / AbstractTracker: Can not create handler inside thread that has not called Looper.prepare () D / OSTracker: OS Event: crash E / AbstractTracker: mTrackerAsyncQueryHandler is null

    
answered by 06.06.2018 в 19:59