-1

I am creating android app. The app store in sqlite database Pet type (like: Dog,Cat , Bird) and Pet Name. I have two tables:

AnimalType.java

 @PrimaryKey(autoGenerate = true)
 int type_id;
 // Type of the animal (cat or dog or bird )
 String type_name;

AnimalName.java

@PrimaryKey(autoGenerate = true)
int name_id;
String animal_name;
// Animal type foreign key
int type_fk;

I have problem displaying data in recyclerview. The problem is i can not to sort or group the data based on Pet type object. (Note that i have created a POJO Model to get and display two different objects (animal_type and animal_name)) This is the Model:

AnimalModel.java

public class AnimalModel {
   public String type_name;
   public String animal_name;
 }

This is how i implemented my recyclerview

AllAnimalAdapter.java

public class AllAnimalAdapter extends RecyclerView.Adapter<AllAnimalAdapter.MyViewHolder> {

Context mContext;
ArrayList<AnimalModel> animalModel_list;

public AllAnimalAdapter(Context context) {
    this.mContext = context;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_all_animals, viewGroup, false);
    MyViewHolder viewHolder = new MyViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    AnimalModel pos = animalModel_list.get(position);
    holder.tv_animalType_var.setText(pos.type_name);
    holder.tv_animalName_var.setText(pos.animal_name);


}

@Override
public int getItemCount() {
    if (animalModel_list == null) {
        return 0;
    } else return animalModel_list.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView tv_animalType_var,tv_animalName_var;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        tv_animalType_var = itemView.findViewById(R.id.tv_animalType_xml);
        tv_animalName_var = itemView.findViewById(R.id.tv_petName_xml);
    }
}

public void setAnimalModel_list(ArrayList<AnimalModel> animalModel_list) {
    this.animalModel_list = animalModel_list;
    notifyDataSetChanged();
 }
}

item_all_animals.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:tool="http://schemas.android.com/apk/res-auto">

<TextView
    android:id="@+id/tv_animalType_xml"
    android:padding="10dp"
    android:textStyle="bold"

    android:text="Animal type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


<TextView
    android:id="@+id/tv_petName_xml"
    android:padding="10dp"
    android:text="Pet Name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

FragmentAllAnimal.java

public class FragmentAllAnimal extends Fragment {

RecyclerView rc_allAnimals_var;
AllAnimalAdapter adapter;
MainViewModel viewModel;


// Empty Constructor
public FragmentAllAnimal() {
}


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable    ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_all_animal, container, false);
    viewModel = ViewModelProviders.of(this).get(MainViewModel.class);


    buildRecyclerView(view);
    setHasOptionsMenu(true);
    setUpViewModel_loadAnimalModel();


    return view;

}

// Building RecyclerView
private void buildRecyclerView(View view) {
    rc_allAnimals_var = view.findViewById(R.id.rc_allAnimals_xml);
    rc_allAnimals_var.setLayoutManager(new LinearLayoutManager(getActivity()));
    adapter = new AllAnimalAdapter(getActivity());
    rc_allAnimals_var.setAdapter(adapter);


}

// Query for animal Model
public void setUpViewModel_loadAnimalModel() {
    viewModel.get_TypeAndName().observe(this, new Observer<List<AnimalModel>>() {
        @Override
        public void onChanged(@Nullable List<AnimalModel> animalModels) {
            adapter.setAnimalModel_list((ArrayList<AnimalModel>) animalModels);
        }
    });


}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_animal_type, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_add_animal_type_xml:
            Intent i = new Intent(getContext(), AddNewAnimalType.class);
            startActivity(i);
            break;
        case R.id.menu_add_pet_name_xml:
            Intent ii = new Intent(getContext(), AddNewPetName.class);
            startActivity(ii);
            break;
    }
    return true;
}

}

This is the actual result

Actual result image

This is how I want to display the data, I want to display animal type (like Cat) as a Header, and group the data based on object type.

Desired output

Desired output image

This is my App at GitHub: https://github.com/Andre112234/Pets

andr111
  • 49
  • 4
  • 2
    Possible duplicate of [How to create RecyclerView with multiple view type?](https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type) – Archie G. Quiñones Apr 23 '19 at 08:19

1 Answers1

-1

you have to use multiple views and their view holders and show them base on their position in recyclerview stack like this

masoud vali
  • 1,528
  • 2
  • 18
  • 29