使用 listView 和适配器显示对象

Display an object with the listView and adapter

我正在编写一个简单的联系人列表应用程序,它允许用户输入姓名和姓氏,将这些项目存储在列表中并将它们显示为 editText 字段下方的列表。 我创建了一个包含名字和姓氏两个字段的对象,并尝试将该对象添加到要显示在按钮下方的列表中。但是,屏幕上什么也没有出现。调试消息显示对象已成功创建并添加到列表中,但我尝试显示对象字段值的方式一定有问题。如果有人能告诉我我在这里做错了什么,我将不胜感激。

这是布局 xml 代码(ListView 部分):

  <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1"
        android:layout_marginTop="20dp"/>

Java部分:

public class Person {
private static String firstname;
private static String lastname;

public Person(String firstname, String lastname) {
    this.firstname = firstname;
    this.lastname = lastname;
}

public static String getFirstname() {
    return firstname;
}

public static String getLastname() {
    return lastname;
}

public String toString(){
    return  getFirstname()+ " " + getLastname();
}

}

和主要功能

public class MainActivity extends AppCompatActivity {


private EditText ent_name;
private EditText ent_surname;
private ListView listView;
private Person person;
private String first_name;
private String last_name;
private ArrayAdapter<Person> adapter;
private List<Person> people = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ent_name = findViewById(R.id.txt_firstName);
    ent_surname = findViewById(R.id.txt_lastName);
    listView = findViewById(R.id.listView1);

    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, people);
    listView.setAdapter(adapter);


}

public void clear(View view) {
    ent_name.setText("");
    ent_surname.setText("");

}

public void add(View view) {
    first_name = ent_name.getText().toString();
    last_name = ent_surname.getText().toString();

    Person person = new Person(first_name, last_name);
    people = Arrays.asList(person);
    adapter.notifyDataSetChanged();
}

}

调试日志

people.get(0) = {Person@11058} "Harry Potter"
this = {MainActivity@11055} 
view = {AppCompatButton@11057} "android.support.v7.widget.AppCompatButton{1bc3388 VFED..C.. ...P.... 720,0-1028,168 #7f070019 app:id/add}"
person = {Person@11058} "Harry Potter"
 firstname = "Harry"
 lastname = "Potter"
 shadow$_klass_ = {Class@10711} "class com.example.lavague.list.Person"
 shadow$_monitor_ = 0
adapter = {ArrayAdapter@11059} 
people = {ArrayList@11056}  size = 1
 0 = {Person@11058} "Harry Potter"

我想 add() 方法应该从 edit texts 中获取名字和姓氏,创建一个新的 Person 并将其添加到 [=14= 的列表中].

如果是这种情况,那么您应该将 people = Arrays.asList(person); 更改为 people.add(person); 现在的方式,它将始终用一个人的新列表替换现有列表。

此外,我看不出有任何理由让 add() 方法接收 View 作为参数。除非您打算做其他事情,否则您可以删除它。

最后,在您发布的代码中,add() 没有在任何地方被调用。也许应该在布局上的按钮中的 OnClickListener 中调用它。

编辑我

您使用 Person class 的方式不应该有静态成员。 静态成员在所有实例之间共享。因此更改名称(如果它是静态的)将更改所有 Person 实例的名称。

public class Person {
    private String firstname;
    private String lastname;

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public static String getFirstname() {
        return firstname;
    }

    public static String getLastname() {
        return lastname;
    }

    public String toString(){
        return  getFirstname()+ " " + getLastname();
    }
}

对于崩溃,请检查左侧按钮的Logcat:

并寻找 Exceptionstack trace
堆栈跟踪具有直到抛出异常为止的所有调用层次结构。 您需要将其添加到问题中。