1

La cosa es que tengo estas tres clases con las que juego:

chatListResponsehandler.java

public class chatListResponseHandler extends AsyncHttpResponseHandler {

    private Context context = null;

    public chatListResponseHandler(Context context) {
        this.context = context;
    }

    @Override
    public void onSuccess(int i, Header[] headers, byte[] bytes) {

        dataChatsUser usersChat= new dataChatsUser();

        Gson gson = new Gson();
        String bytesResponse = new String(bytes);

        chatListData listChat = gson.fromJson(bytesResponse, chatListData.class);
        String array[]=new String[listChat.getChats().size()];
        for (int x = 0; x < listChat.getChats().size(); x++) {

            String pepe = listChat.getChats().get(x).getUsers().get(1).getName();
            array[x]=pepe;
        }
        usersChat.setUsernames(array);

        Intent listChatAd = new Intent(this.context, listChatAdapter.class);
        this.context.startActivity(listChatAd);
    }
}

listChatAdapter.java

public class listChatAdapter extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(getApplicationContext(), "llega", Toast.LENGTH_SHORT).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_chat_layout);

            dataChatsUser dataChatUs =new dataChatsUser();
            String[] array = dataChatUs.getUsernames();
    }
}

dataChatsUser.java

public class dataChatsUser {
        private String[] usernames;

        public void setUsernames(String[] usernames){
                this.usernames=usernames;
        }
        public String[] getUsernames(){
                return this.usernames;
        }
}

La cuestión es que recojo los datos que pido de modo REST a un servidor y de ahí voy al onSuccess del chatListResponseHandler, los datos de estos usuarios los recojo en un array, y se los paso a la clase dataChatsUser con la intención de guardarlos y que a la hora de que se guarden cambiar de Activity y recogerlos con un getUsernames() en el chatListAdapter. Pero todo el rato el valor me da null. ¿Por qué puede ser? ¿Se hace como lo estoy tratando de hacer?

1 respuesta 1

2

Pero todo el rato el valor me da null. ¿Por qué puede ser?

En la clase listChatAdapter.java estas creando una nueva instancia, la cual no es a la que originalmente guardaste los valores, por esa razón no obtienes datos.

 dataChatsUser dataChatUs =new dataChatsUser();
 String[] array = dataChatUs.getUsernames();

Como opción puedes crear una variable publica usersChat en la clase chatListResponseHandler, en la cual guardas los valores pero esta variable podrá ser leída por otra clase y un método público para extraer este valor getDataChatUs().

public class chatListResponseHandler extends AsyncHttpResponseHandler {

    private Context context = null;
    public dataChatsUser  usersChat;

    private Context context = null;

  public chatListResponseHandler(Context context) {
    this.context = context;
}

  @Override
  public void onSuccess(int i, Header[] headers, byte[] bytes) {

       //dataChatsUser usersChat= new dataChatsUser();
        usersChat= new dataChatsUser();
        ...
        ...
      String pepe = usersChat.setUsernames(array);
       ...
   }

  public dataChatsUser getDataChatUs(){
      return usersChat;
  } 
}

De esta forma mediante el método getDataChatUs() podrías obtener los datos.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(getApplicationContext(), "llega", Toast.LENGTH_SHORT).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_chat_layout);

            //dataChatsUser dataChatUs = new dataChatsUser();
            //String[] array = dataChatUs.getUsernames();
           String[] array = chatListResponseHandler.getDataChatUs();
    }
}
2
  • ahora el único problema que me aparece en la parte de chatListResponseHandler.getDataChatUs(); me aparece que "el método estático getDataChatUS no puede ser referenciado a un método estático.
    – Zaldaiz
    el 21 dic. 2016 a las 1:50
  • ups! public static dataChatsUser getDataChatUs(){ @Zaldaiz
    – Jorgesys
    el 21 dic. 2016 a las 15:23

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.