Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Recyclerview中显示多个对象类型_Android_Android Recyclerview - Fatal编程技术网

Android 在Recyclerview中显示多个对象类型

Android 在Recyclerview中显示多个对象类型,android,android-recyclerview,Android,Android Recyclerview,我正在创建android应用程序。sqlite数据库中的应用商店宠物类型(如:狗、猫、鸟)和宠物名称。 我有两张桌子: AnimalType.java @PrimaryKey(autoGenerate = true) int type_id; // Type of the animal (cat or dog or bird ) String type_name; @PrimaryKey(autoGenerate = true) int name_id; String animal_n

我正在创建android应用程序。sqlite数据库中的应用商店宠物类型(如:狗、猫、鸟)和宠物名称。 我有两张桌子:

AnimalType.java

 @PrimaryKey(autoGenerate = true)
 int type_id;
 // Type of the animal (cat or dog or bird )
 String type_name;
@PrimaryKey(autoGenerate = true)
int name_id;
String animal_name;
// Animal type foreign key
int type_fk;
public class AnimalModel {
   public String type_name;
   public String animal_name;
 }
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();
 }
}
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;
}

}
AnimalName.java

 @PrimaryKey(autoGenerate = true)
 int type_id;
 // Type of the animal (cat or dog or bird )
 String type_name;
@PrimaryKey(autoGenerate = true)
int name_id;
String animal_name;
// Animal type foreign key
int type_fk;
public class AnimalModel {
   public String type_name;
   public String animal_name;
 }
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();
 }
}
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;
}

}
我在recyclerview中显示数据时遇到问题。 问题是我无法根据Pet类型对象对数据进行排序或分组。 (请注意,我创建了一个POJO模型来获取和显示两个不同的对象(animal_type和animal_name)) 这就是模型:

AnimalModel.java

 @PrimaryKey(autoGenerate = true)
 int type_id;
 // Type of the animal (cat or dog or bird )
 String type_name;
@PrimaryKey(autoGenerate = true)
int name_id;
String animal_name;
// Animal type foreign key
int type_fk;
public class AnimalModel {
   public String type_name;
   public String animal_name;
 }
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();
 }
}
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;
}

}
这就是我实现我的recyclerview的方式

AllAnimalAdapter.java

 @PrimaryKey(autoGenerate = true)
 int type_id;
 // Type of the animal (cat or dog or bird )
 String type_name;
@PrimaryKey(autoGenerate = true)
int name_id;
String animal_name;
// Animal type foreign key
int type_fk;
public class AnimalModel {
   public String type_name;
   public String animal_name;
 }
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();
 }
}
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;
}

}
公共类AllAnimalAdapter扩展了RecyclerView.Adapter

这就是我想显示数据的方式,我想显示动物类型(如猫)作为标题,并根据对象类型对数据进行分组

期望输出

这是我在GitHub的应用程序:

您必须使用多个视图及其视图持有者,并根据它们在recyclerview堆栈中的位置显示它们,就像