【问题标题】:How to manage a list of objects?如何管理对象列表?
【发布时间】:2018-07-08 11:23:09
【问题描述】:

我想制作一个 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,一个是他们的名字?

非常感谢!

【问题讨论】:

  • 我认为问题太宽泛了。试着缩小你的疑虑,专注于一个问题。解释清楚,解释你的尝试,你会得到具体的答案。对其他疑问(如果有的话)做同样的事情。在 Stackoverflow 上,我们专注于简单的 QA 结构,一个包含多个问题的问题通常只会让回答的人和将来寻找答案的人感到困惑。祝你好运!
  • 您需要了解RecyclerView。有很多很好的文字和视频教程,这里就不一一解释了。

标签: java android listview scrollview


【解决方案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>

适配器类:

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

    }

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多