Answer the question
In order to leave comments, you need to log in
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:
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);
}
}
}
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);
}
}
}
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) {
}
});
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question