A
A
Andrey Goroshko2018-08-09 12:11:57
Android
Andrey Goroshko, 2018-08-09 12:11:57

Why do two identical classes work differently?

In my application, I need to get two lists of applications, incoming and outgoing, these two lists fill two RecyclerView lists, and then when I click on an element, I go to another activity. To populate these lists, I use two adapters:

  1. For incoming:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageHolder> {
    private List<Message> messageList;
    private Context ctx;
    private static final String keyMessageId = "message_id";

    MessageAdapter(List<Message> messageList, Context ctx) {
        this.messageList = messageList;
        this.ctx = ctx;
    }

    @NonNull
    @Override
    public MessageHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_of_rec_m, viewGroup, false);
        return new MessageHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final MessageHolder holder, int position) {
        final Message message = messageList.get(position);

        holder.tvSubject.setText(message.getSubject());
        holder.tvFrom.setText(message.getSender_name());
        holder.tvDate.setText(message.getDate());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(ctx,MessageShow.class);
                intent.putExtra(keyMessageId,message.getId());
                ctx.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }


    class MessageHolder extends RecyclerView.ViewHolder {

        final TextView tvFrom, tvSubject, tvDate;

        MessageHolder (View itemView) {
            super(itemView);
            tvFrom = itemView.findViewById(R.id.tv_from);
            tvSubject = itemView.findViewById(R.id.tv_subject);
            tvDate = itemView.findViewById(R.id.tv_date);
        }
    }
}

For outgoing:
public class MessageAdapter2 extends RecyclerView.Adapter<MessageAdapter2.SenderHolder> {
    private List<Message> messageList;
    private Context ctx;
    private static final String keyMessageId = "message_id";

    MessageAdapter2(List<Message> messageList, Context ctx) {
        this.messageList = messageList;
        this.ctx = ctx;
    }

    @NonNull
    @Override
    public SenderHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_of_rec_m, viewGroup, false);
        return new SenderHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final SenderHolder holder, int position) {
        final Message message = messageList.get(position);

        holder.tvSubject.setText(message.getSubject());
        holder.tvFrom.setText(message.getReceiver_name());
        holder.tvDate.setText(message.getDate());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(ctx,MessageShow.class);
                intent.putExtra(keyMessageId,message.getId());
                ctx.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }


    class SenderHolder extends RecyclerView.ViewHolder {

        final TextView tvFrom, tvSubject, tvDate;

        SenderHolder (View itemView) {
            super(itemView);
            tvFrom = itemView.findViewById(R.id.tv_from);
            tvSubject = itemView.findViewById(R.id.tv_subject);
            tvDate = itemView.findViewById(R.id.tv_date);
        }
    }
}

the activity to which we switch when selecting a message:
public class MessageShow extends AppCompatActivity {

    private static final String keyMessageId = "message_id";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_show);
        Integer id = getIntent().getIntExtra(keyMessageId , 0);
        showMess(id);
    }

    private void showMess(Integer id) {
        final TextView tvPerson, tvTheme, tvBody;

        tvTheme = findViewById(R.id.theme);
        tvPerson = findViewById(R.id.sender);
        tvBody = findViewById(R.id.date_of);

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://сервер/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        String ACCESS_TOKEN = "access_token";
        SharedPreferences tok_pref = getSharedPreferences(ACCESS_TOKEN, Context.MODE_PRIVATE);
        String access_token = tok_pref.getString(ACCESS_TOKEN, "");


        APIService mAPIService = retrofit.create(APIService.class);
        mAPIService.messView("Bearer " + access_token, id, 0, 1).enqueue(new Callback<ViewMessage>() {
            @Override
            public void onResponse(@NonNull Call<ViewMessage> call, @NonNull Response<ViewMessage> response) {

                if (response.isSuccessful()) {
                    tvPerson.setText(response.body().getName());
                }
            }

            @Override
            public void onFailure(@NonNull Call<ViewMessage> call, @NonNull Throwable t) {

            }
        });
    }

}

but I can’t understand, it seems like I have two identical classes, but for some reason when I click on an element of the incoming list, I can normally pull out the message id and pass it to another activity for the request, but for some reason such a system does not work with the outgoing list, it seems like something is being transmitted, but if you look at the outgoing request logs, the server writes an error that it cannot find the message id. Maybe somewhere I didn’t add something)) I already tried to make two different identifiers for the intent, that is, different variables come from different adapters, but that didn’t help either.
I will be glad to councils, criticism and any other help. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question