我想制作一个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
列表,上面有动物“名字”。我在Internet上找到了这段代码,但是我不太了解一切如何工作,因此无法适应它来使项目可点击,...
我想知道的是:
ListView
是更好的解决方案还是使用ScrollView
是更好的主意?Animals
放在列表中,所以我应该有两个列表:一个带有Animals
的列表,另一个带有其名称的列表?非常感谢!
答案 0 :(得分:1)
使用这些,这是一个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>
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));
}