我正在创建android应用。该应用商店位于sqlite数据库中的Pet类型(如Dog,Cat,Bird)和Pet Name。 我有两个表:
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;
我在recyclerview中显示数据时遇到问题。 问题是我无法根据Pet类型的对象对数据进行排序或分组。 (请注意,我已经创建了一个POJO模型来获取和显示两个不同的对象(animal_type和animal_name)) 这是模型:
AnimalModel.java
public class AnimalModel {
public String type_name;
public String animal_name;
}
这就是我实施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;
}
}
这是实际结果
这就是我要显示数据的方式,我想将动物类型(如Cat)显示为页眉,然后根据对象类型对数据进行分组。
所需的输出
这是我在GitHub上的应用程序: https://github.com/Andre112234/Pets