I would like to show the data from two DBs in one RecyclerView at the same time.
There are two tables like this, and the names of the players on the baseball team and the basketball team are the same. When I search for the player's name on Android, I want to show the baseball team's name and batting average and the basketball team's name and position.
I wanted this kind of result, but it doesn't come out. I found out that you can designate a ViewType, but I'm not sure if it's right to specify it like this.
The bottom is MainActivity.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
SearchView searchView;
ApiInterface apiInterface;
Adapter adapter;
List<BaseBallInfo> baseBallInfoList;
List<BasketBallInfo> basketBallInfoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
searchView = findViewById(R.id.searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
search(s);
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
}
private void search(String name) {
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
Call<List<BaseBallInfo>> call = apiInterface.getBaseBallInfo(name);
call.enqueue(new Callback<List<BaseBallInfo>>() {
@Override
public void onResponse(Call<List<BaseBallInfo>> call, Response<List<BaseBallInfo>> response) {
baseBallInfoList = response.body();
adapter = new Adapter(MainActivity.this, baseBallInfoList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<BaseBallInfo>> call, Throwable t) {
Log.e("MainActivity", t.toString());
}
});
Call<List<BasketBallInfo>> call2 = apiInterface.getBasketBallInfo(name);
call2.enqueue(new Callback<List<BasketBallInfo>>() {
@Override
public void onResponse(Call<List<BasketBallInfo>> call, Response<List<BasketBallInfo>> response) {
basketBallInfoList = response.body();
adapter = new Adapter(MainActivity.this, basketBallInfoList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<BasketBallInfo>> call, Throwable t) {
Log.e("MainActivity", t.toString());
}
});
}
}
Below is the Adapter in RecyclerView.
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List list;
public Adapter(Context context, List list) {
this.context = context;
this.list = list;
}
@Override
public int getItemViewType(int position) {
if(list.get(position) instanceof BaseBallInfo) {
return 0;
}
return 1;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(this.getItemViewType(viewType) == 0) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_baseball, parent, false);
BaseBallViewHolder holder = new BaseBallViewHolder(view);
return holder;
} } else {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_basketball, parent, false);
BasketBallViewHolder holder = new BasketBallViewHolder(view);
return holder;
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if(this.getItemViewType(position) == 0) {
BaseBallInfo baseBallInfo = (BaseBallInfo)list.get(position);
BaseBallViewHolder holder = (BaseBallViewHolder) viewHolder;
holder.nameTextView.setText(baseBallInfo.getName());
holder.battingTextView.setText(baseBallInfo.getBatting_avg());
} } else {
BasketBallInfo basketBallInfo = (BasketBallInfo)list.get(position);
BasketBallViewHolder holder = (BasketBallViewHolder) viewHolder;
holder.name2TextView.setText(basketBallInfo.getName());
holder.positionTextView.setText(basketBallInfo.getPos());
}
}
@Override
public int getItemCount() {
return list.size();
}
public static class BaseBallViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView, battingTextView;
public BaseBallViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.nameTextView);
battingTextView = itemView.findViewById(R.id.battingTextView);
}
}
public static class BasketBallViewHolder extends RecyclerView.ViewHolder {
TextView name2TextView, positionTextView;
public BasketBallViewHolder(@NonNull View itemView) {
super(itemView);
name2TextView = itemView.findViewById(R.id.name2TextView);
positionTextView = itemView.findViewById(R.id.positionTextView);
}
}
}
I'm using php as an intermediate language, but the php results come out well. Help me.
android retrofit2 recyclerview
There seems to be no need to use the view type in the view holder or divide the list into two.
Name - Key to be retrieved Data - Value to show
You can show two like this, and if you don't need to double-check them, I think you can put them all in one list (because they have the same name) and show them as they are.
© 2024 OneMinuteCode. All rights reserved.