我得到了一个空的片段活动,带有cardview的recyclerview数据如何出现在我的家庭片段上,或者如何在我的片段家庭上调用我的cardviewadapter
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth auth;
private TextView mTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fl_container,new FragmentHome());
fragmentTransaction.commit();
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
auth = FirebaseAuth.getInstance();
// kita set default nya Home Fragment
loadFragment(new FragmentHome());
// inisialisasi BottomNavigaionView
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
// beri listener pada saat item/menu bottomnavigation terpilih
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_profile:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_pengaduan:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
return true;
}
return false;
}
};
// method untuk load fragment yang sesuai
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fl_container, fragment)
.commit();
return true;
}
return false;
}
// method listener untuk logika pemilihan
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new FragmentHome();
break;
case R.id.navigation_profile:
fragment = new FragmentProfile();
break;
case R.id.navigation_pengaduan:
fragment = new FragmentPengaduan();
break;
case R.id.navigation_more:
fragment = new FragmentMore();
break;
}
return loadFragment(fragment);
}
@Override
protected void onPause() {
super.onPause();
auth.signOut();
}
}
FragmentHome.java
public class FragmentHome extends Fragment {
View v;
private RecyclerView recyclerView;
private ArrayList<Berita> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container,false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
ItemCardViewAdapter itemCardViewAdapter = new ItemCardViewAdapter(getActivity());
recyclerView.setAdapter(itemCardViewAdapter);
return v;
}
}
ItemCardViewAdapter.java
public class ItemCardViewAdapter extends RecyclerView.Adapter<ItemCardViewAdapter.CardViewHolder> {
private Context context;
private ArrayList<Berita> listBerita;
public ItemCardViewAdapter(Context context) { this.context = context; }
public ArrayList<Berita> getListBerita() {
return listBerita;
}
public void setListBerita(ArrayList<Berita> listBerita) {
this.listBerita = listBerita;
}
@NonNull
@Override
public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview, parent,false);
return new CardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull CardViewHolder holder, final int position) {
holder.tvName.setText(getListBerita().get(position).getName());
holder.tvDescription.setText(getListBerita().get(position).getDescription());
Glide.with(context).load(getListBerita().get(position).getPhoto()).into(holder.imgPhoto);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Kamu Memilih " + getListBerita().get(position).getName(),Toast.LENGTH_SHORT).show();
Intent detailActivity = new Intent(context, DetailActivity.class);
detailActivity.putExtra(DetailActivity.EXTRA_BERITA,listBerita.get(position));
context.startActivity(detailActivity);
}
});
}
@Override
public int getItemCount() {
return getListBerita().size();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvDescription;
Button button;
public CardViewHolder(@NonNull View itemView) {
super(itemView);
context = itemView.getContext();
imgPhoto = itemView.findViewById(R.id.img_card);
tvName = itemView.findViewById(R.id.tv_name_card);
tvDescription = itemView.findViewById(R.id.tv_desc_card);
button = itemView.findViewById(R.id.button);
}
}
}
Berita.java
public class Berita implements Parcelable {
private String name, elevation, photo, description, location, latitude, longitude;
public Berita() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getElevation() {
return elevation;
}
void setElevation(String elevation) {
this.elevation = elevation;
}
public String getPhoto() {
return photo;
}
void setPhoto(String photo) {
this.photo = photo;
}
public String getDescription() {
return description;
}
void setDescription(String description) {
this.description = description;
}
public String getLocation() {
return location;
}
void setLocation(String location) {
this.location = location;
}
public String getLatitude() {
return latitude;
}
void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
void setLongitude(String longitude) {
this.longitude = longitude;
}
Berita(Parcel in) {
name = in.readString();
elevation = in.readString();
photo = in.readString();
description = in.readString();
location = in.readString();
latitude = in.readString();
longitude = in.readString();
}
public static final Creator<Berita> CREATOR = new Creator<Berita>() {
@Override
public Berita createFromParcel(Parcel in) {
return new Berita(in);
}
@Override
public Berita[] newArray(int size) {
return new Berita[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(elevation);
parcel.writeString(photo);
parcel.writeString(description);
parcel.writeString(location);
parcel.writeString(latitude);
parcel.writeString(longitude);
}
}
注意:我的数据列表位于BeritaData.java中,对于卡视图将显示一些信息
如何使我的数据显示在片段主页选项卡上