When you press the button on the main screen, it goes to the animal hospital list screen I was compiling a list of veterinary hospitals with Recyclerview. (I have created intents on the main screen, but I have not yet created intents on the animal hospital list screen.)
All other codes have not failed, but the getApplicationContext() part will fail.
logcat: 'hospitalListAdapter(android.content.Context, java.util.ArrayList<com.example.project.hospitalListItem>)' in 'com.example.project.hospitalListAdapter' cannot be applied to '(android.content.Context)'
hospitalActivity.java (List of Animal Hospitals)
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class hospitalActivity extends AppCompatActivity {
RecyclerView hospital_list;
hospitalListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hospital);
hospital_list = findViewById(R.id.hospital_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
hospital_list.setLayoutManager(layoutManager);
adapter = new hospitalListAdapter(getApplicationContext());
hospital_list.setAdapter(adapter);
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
}
}
hospitalListItem.java (ListItem)
public class hospitalListItem {
int resId;
String title;
String address;
String phone;
String guri;
public hospitalListItem(int resId, String title, String address, String phone, String guri) {
this.resId = resId;
this.title = title;
this.address = address;
this.phone = phone;
this.guri = guri;
}
}
hospitalListAdapter.java (ListAdapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class hospitalListAdapter extends RecyclerView.Adapter<hospitalListAdapter.ViewHolder> {
Context mContext;
ArrayList<hospitalListItem> items = new ArrayList<>();
public hospitalListAdapter(Context mContext, ArrayList<hospitalListItem> items) {
this.mContext = mContext;
this.items = items;
}
@NonNull
@Override
public hospitalListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.hospital_design, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull hospitalListAdapter.ViewHolder holder, int position) {
hospitalListItem item = items.get(position);
holder.setItem(item);
}
@Override
public int getItemCount() {
return items.size();
}
public void addItem(hospitalListItem item){
items.add(item);
}
static class ViewHolder extends RecyclerView.ViewHolder{
ImageView listEdit_tv;
TextView title_tv;
TextView addr_tv;
TextView phone_tv;
TextView guri_tv;
public ViewHolder(View itemView) {
super(itemView);
listEdit_tv = itemView.findViewById(R.id.listEdit_tv);
title_tv = itemView.findViewById(R.id.title_tv);
addr_tv = itemView.findViewById(R.id.addr_tv);
phone_tv = itemView.findViewById(R.id.phone_tv);
guri_tv = itemView.findViewById(R.id.guri_tv);
}
public void setItem(hospitalListItem item){
listEdit_tv.setImageResource(item.resId);
title_tv.setText(item.title);
addr_tv.setText(item.address);
phone_tv.setText(item.phone);
guri_tv.setText(item.guri);
}
}
}
I need your help. I'd appreciate your help.
enter image description here enter image description here
CodePudding user response:
public hospitalListAdapter(Context mContext, ArrayList<hospitalListItem> items)
This is the constructor of your hospitalListAdapter Class. Whenever, you want to create objects of a class you need to call its constructor and pass the parameters. Here the constructor requires a Context and a ArrayList<>, so you need to pass both but you are only passing the context.
// also pass the list to the constructor here
adapter = new hospitalListAdapter(getApplicationContext());
Correct way:
// here I am creating an empty list and passing it to adapter as required.
ArrayList<hospitalListItem> list = new ArrayList<hospitalListItem>();
adapter = new hospitalListAdapter(getApplicationContext(), list);
Also, Instead of passing items one by one to your adapter you can first add them to your list and then pass the whole list itself, as shown above.
// passing items one by one to the list
adapter.addItem(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
Do this instead.
// create a list
ArrayList<hospitalListItem> list = new ArrayList<hospitalListItem>();
// add items to the list
list.add(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
list.add(new hospitalListItem(R.drawable.nuribom, "동물병원", "종로구","02-33", "어쩌구저쩌구"));
// add remaining items here
// Finally, pass the whole list to adapter at once.
adapter = new hospitalListAdapter(getApplicationContext(), list);