0

I'm working on a little project in android studio and I have a problem, I'm using an ArrayAdapter to catch information from a JSON code and now I can display the data but when I select an item it doesn't show in the display of the spinner, I hope you can help me with this here is my code:

public class Asistencia extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    ProgressDialog progressDialog;
    private String END_POINT_URL = "http://192.168.0.6:1434/Bitacora/Usuarios.php";
    private Spinner p1;
    private List<String> listu =new ArrayList<String>();
    private String[] listu1 = new String [100];

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

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Por favor espere...");
        progressDialog.setCancelable(false);

        p1 = (Spinner) findViewById(R.id.spinnerUsuario);
        ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listu);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
        p1.setAdapter(dataAdapter);


        p1.setOnItemSelectedListener(this);
        dataAdapter.notifyDataSetChanged();

        callRegisterSpinnerU();
    }
    @Override public void onItemSelected(AdapterView parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    private void callRegisterSpinnerU()
    {
        progressDialog.show();

        final AsyncHttpClient client = new AsyncHttpClient();

        client.post(END_POINT_URL, new AsyncHttpResponseHandler(){

            @Override
            public void onSuccess(int statusCode, String content) {
                progressDialog.hide();


                try {

                    JSONObject obj = new JSONObject(content);
                    final JSONArray jsonArray = obj.getJSONArray("users");
                    int length = jsonArray.length();
                    for (int i = 0; i < length; i++)
                    {
                        JSONObject user = jsonArray.getJSONObject(i);
                        listu.add(user.getString("Usuario"));

                    }
                }catch(JSONException e){
                    Toast.makeText(getApplicationContext(), "Error al enviar su solicitud, Verifique el Json" + e.toString(), Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFailure(int statusCode, Throwable error, String content) {
                progressDialog.hide();

                if(statusCode == 404){
                    Toast.makeText(getApplicationContext(), "No se envio la solicitud", Toast.LENGTH_LONG).show();
                }
                else if(statusCode == 500){
                    Toast.makeText(getApplicationContext(), "Algo ocurrio al final", Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(getApplicationContext(), "Error: El dispositivo tal vez no este conectado a la red o el server se encuentra sin funcionar", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

when I try to select an item from the spinner I receive this message in the logcat:

12-09 00:24:56.063 8171-8171/com.example.crisis.bitacora W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

Sorry for my bad English :(.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96

2 Answers2

0

You didn't notify dataAdapter when listu Changed;

Try called dataAdapter.notifyDataSetChanged(); when get data success from network.

Mick
  • 61
  • 3
0

Just Add :

  public void onItemSelected(AdapterView parent, View view, int position, long id) {


    p1.setSelection(position);
    }