我想制作一个Android(我是初学者)应用程序,我可以在其中管理对象列表(例如Animals)。我需要能够点击列表中的每个动物来删除它或编辑它的属性,问题是我不知道该怎么做。
这是我的Animal类(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
):
package fr.lap.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = findViewById(R.id.lv);
String[] animals = new String[15];
for (int i = 0;i < 15; i++)
animals[i] = "animal " + i;
final List<String> tests_list = new ArrayList<String>(Arrays.asList(animals));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tests_list);
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
这是activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
所以你可以看到,我有一个动物“名字”的String
列表。我在互联网上找到了这个代码但是我并不真正理解一切是如何工作的所以我无法调整它以使项目可点击,...
我想知道的是:
ListView
更好的解决方案或使用ScrollView
会是一个更好的主意?Animals
放在列表中,所以我应该有两个列表:一个用Animals
,一个用他们的名字?非常感谢 !
使用这些,它是一个recyclerView和它的适配器。
main 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>
cell 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>
Adapter类:
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));
}