in item_common.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
android:elevation="6dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/parent_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8DFA91"
android:padding="15dp">
<TextView
android:id="@+id/txt_common_title_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_common_sex_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_common_title_item"
android:text="TextView"
android:textColor="@color/black" />
<TextView
android:id="@+id/txt_pt_id_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_common_sex_item"
android:text="TextView"
android:visibility="invisible" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
in MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
CommonAdapter commonAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv1);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
FirebaseRecyclerOptions<CommonModel> options = new FirebaseRecyclerOptions.Builder<CommonModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("SiddhaMedicine").child("Surgery"), CommonModel.class)
.build();
commonAdapter = new CommonAdapter(options, this);
recyclerView.setAdapter(commonAdapter);
}
@Override
protected void onStart() {
super.onStart();
commonAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
commonAdapter.stopListening();
}
}
in CommonModel.java
public class CommonModel {
String Id, Title, Body;
public CommonModel() {
}
public CommonModel(String id, String title, String body) {
Id = id;
Title = title;
Body = body;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getBody() {
return Body;
}
public void setBody(String body) {
Body = body;
}
}
in CommonAdapter.java
public class CommonAdapter extends FirebaseRecyclerAdapter<CommonModel, CommonAdapter.myViewHolder> {
private Context mContext;
/**
* Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
* {@link FirebaseRecyclerOptions} for configuration options.
*
* @param options
*/
public CommonAdapter(@NonNull FirebaseRecyclerOptions<CommonModel> options, Context context) {
super(options);
mContext = context;
}
@Override
protected void onBindViewHolder(@NonNull CommonAdapter.myViewHolder holder, int position, @NonNull CommonModel model) {
holder.cName.setText(model.getTitle());
holder.cSex.setText(model.getId());
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, model.getTitle(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(mContext, BodyActivity.class);
intent.putExtra("bId", model.getId());
mContext.startActivity(intent);
}
});
}
@NonNull
@Override
public CommonAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_common,parent,false);
return new CommonAdapter.myViewHolder(view);
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView cName, cSex;
RelativeLayout relativeLayout;
public myViewHolder(@NonNull View itemView) {
super(itemView);
cName = (TextView) itemView.findViewById(R.id.txt_common_title_item);
cSex = (TextView) itemView.findViewById(R.id.txt_common_sex_item);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.parent_layout);
}
}
}
in build.gradle(:app)
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.firebaseui:firebase-ui-database:7.1.1'
implementation "androidx.recyclerview:recyclerview:1.2.1"
0 Comments