Java 如何管理对象列表?
我想制作一个Android(我是初学者)应用程序,在其中我可以管理对象列表(例如动物)。我需要能够点击列表中的每一个动物来删除它或编辑它的属性,问题是我不知道怎么做 下面是我的动物类(Java 如何管理对象列表?,java,android,listview,scrollview,Java,Android,Listview,Scrollview,我想制作一个Android(我是初学者)应用程序,在其中我可以管理对象列表(例如动物)。我需要能够点击列表中的每一个动物来删除它或编辑它的属性,问题是我不知道怎么做 下面是我的动物类(Animal.java),您可以看到它非常基本: public class Animal { private String name; private int age; private float size; private double weight; public An
Animal.java
),您可以看到它非常基本:
public class Animal {
private String name;
private int age;
private float size;
private double weight;
public Animal(String name, int age, float size, double weight) {
this.name = name;
this.age = age;
this.size = size;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSize() {
return size;
}
public void setSize(float size) {
this.size = size;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
这是我的MainActivity类(MainActivity.java
):
如你所见,我有一个带有动物“名字”的字符串的列表。我在互联网上找到了这段代码,但我真的不明白每件事是如何运作的,所以我无法调整它,使项目可点击
我想知道的是:
- 在我的例子中,
ListView
是更好的解决方案还是使用ScrollView
更好
- 如何使列表中的项目可单击(以打开新活动并能够访问所选项目的“控制面板”)
- 如何添加和删除列表中的项目
- 我想我不能把
动物
放在列表中,所以我应该有两个列表:一个是动物
列表,另一个是它们的名字列表
非常感谢 使用这些,它是一个recyclerView和它的适配器
主xml:
<RelativeLayout 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">
<android.support.v7.widget.RecyclerView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
单元格xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
适配器类:
public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder> {
public class AnimalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView name;
NewsItemViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
///do what you want when clicking on your animal object
}
}
public List<Animal> animalList = new ArrayList<>();
public AnimalAdapter(List<Animal> animalList){
this.animalList = animalList;
}
@Override
public int getItemCount() {
return newsList.size();
}
@Override
public AnimalViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView;
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_animal, viewGroup, false);
return new AnimalViewHolder(itemView);
}
@Override
public void onBindViewHolder(AnimalViewHolder holder, int i) {
holder.name.setText(animalList.get(i).getName());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
公共类AnimalAdapter扩展了RecyclerView.Adapter{
公共类AnimalViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
文本视图名称;
NewsItemViewHolder(查看项目视图){
超级(项目视图);
name=(TextView)itemView.findViewById(R.id.name);
setOnClickListener(这个);
}
@凌驾
公共void onClick(视图v){
///单击动物对象时,执行所需操作
}
}
public List animalList=new ArrayList();
公共动物适配器(列出动物列表){
this.animalist=动物主义者;
}
@凌驾
public int getItemCount(){
返回newsList.size();
}
@凌驾
public AnimalViewHolder onCreateViewHolder(视图组视图组,int视图类型){
查看项目视图;
itemView=LayoutInflater.from(viewGroup.getContext()).flate(R.layout.cell\u动物,viewGroup,false);
返回新的AnimalViewHolder(itemView);
}
@凌驾
BindViewHolder上的公共无效(AnimalViewHolder holder,int i){
holder.name.setText(animalList.get(i.getName());
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
}
您的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView lv = findViewById(R.id.lv);
List<Animal> animals = new ArrayList<>();
for (int i = 0;i < 15; i++){
///addd your animals like:
Animal rabbit = new Animal("rabbit",2,1,15);
animals.add(rabbit)
}
lv.setLayoutManager(new LinearLayoutManager(this));
lv.setAdapter(new AnimalAdapter(animals));
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView lv=findViewById(R.id.lv);
列出动物=新建ArrayList();
对于(int i=0;i<15;i++){
///添加您的动物,如:
动物兔子=新动物(“兔子”,2,1,15);
动物。添加(兔子)
}
lv.setLayoutManager(新直线布局经理(本));
lv.setAdapter(新动物适应器(动物));
}
我认为这个问题太宽泛了。试着缩小你的怀疑范围,把注意力集中在一个问题上。好好解释,解释你尝试了什么,你会得到具体的答案。如果有其他疑问,也要这样做。关于Stackoverflow,我们将重点放在简单的QA结构上,一个包含多个问题的问题通常会让回答问题的人和将来寻找答案的人感到困惑。祝你好运您需要了解RecyclerView
。有很多关于它的好的文本和视频教程,所以我不在这里解释。
public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalViewHolder> {
public class AnimalViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView name;
NewsItemViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
///do what you want when clicking on your animal object
}
}
public List<Animal> animalList = new ArrayList<>();
public AnimalAdapter(List<Animal> animalList){
this.animalList = animalList;
}
@Override
public int getItemCount() {
return newsList.size();
}
@Override
public AnimalViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView;
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_animal, viewGroup, false);
return new AnimalViewHolder(itemView);
}
@Override
public void onBindViewHolder(AnimalViewHolder holder, int i) {
holder.name.setText(animalList.get(i).getName());
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView lv = findViewById(R.id.lv);
List<Animal> animals = new ArrayList<>();
for (int i = 0;i < 15; i++){
///addd your animals like:
Animal rabbit = new Animal("rabbit",2,1,15);
animals.add(rabbit)
}
lv.setLayoutManager(new LinearLayoutManager(this));
lv.setAdapter(new AnimalAdapter(animals));
}