java - 如何管理对象列表?

标签 java android listview scrollview

我想制作一个 Android(我是初学者)应用程序,我可以在其中管理对象列表(例如动物)。我需要能够单击列表中的每个动物以将其删除或编辑其属性,问题是我不知道该怎么做。

这是我的 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>

The application result

如您所见,我有一个包含动物“名称”的String列表。我在互联网上找到了这段代码,但我并不真正理解一切是如何工作的,所以我无法调整它以使项目可点击,...

我想知道的是:

  • 就我而言,ListView 是更好的解决方案还是使用 ScrollView 是更好的主意?
  • 如何使列表中的项目可点击(打开新的 Activity 并能够访问所选项目的“控制面板”)
  • 如何添加和删除列表中的项目
  • 我想我无法将Animals放入列表中,所以我应该有两个列表:一个包含Animals,另一个包含它们的名字?

非常感谢!

最佳答案

使用这些,它是一个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);
    }

}

您的 Activity :

@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));

    }

关于java - 如何管理对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51231486/

相关文章:

android - 当我在我的第一个 fragment 上按下后退按钮时,应用程序应该退出

listview - Flutter - 为每个 ListView 项运行异步函数

java - 我已经编写了这个成员(member)资格类(class),我如何创建一个方法来查找给予成员(member)的特定ID?

java - 延伸 2 节课 - 填空 - 最后一次考试排练

android - systrace:HTML 输出的跟踪结果格式无效

android - 拖放动态添加的编辑 TextView

android - 如何从android ListView 中获取值?

c# - 在 ListView EmptyDataTemplate 中查找控件

java - 我怎样才能用按位和xpath和java在xml中查找?

java - 2037-10-18 巴西利亚夏令时过渡