【问题标题】:Display an object with the listView and adapter使用 listView 和适配器显示对象
【发布时间】:2019-02-21 11:51:18
【问题描述】:

我正在编写一个简单的联系人列表应用程序,它允许用户输入姓名和姓氏,将这些项目存储在列表中,并将它们显示为 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() 在哪里被调用?

标签: java android listview android-adapter


【解决方案1】:

我猜add() 方法应该是从edit texts 中获取姓名和姓氏,创建一个新的Person 并将其添加到people 的列表中。

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

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

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

编辑我

你使用 Person 类的方式不应该有静态成员。 静态成员在所有实例之间共享。因此,如果更改名称是静态的,则会更改所有 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
堆栈跟踪具有直到引发异常的所有调用层次。 您需要将其添加到问题中。

【讨论】:

  • 没错,这个想法是创建一个新的Person并添加到列表中。很抱歉,我忘记包含 add 方法的代码 - 它是绑定到按钮的函数,可在单击时添加输入的值。我已将 asList 更改为 add() 并且应用程序崩溃了。
  • 同样你使用Person的方式,属性和方法不应该是静态的。
  • 我希望,我做对了:people = {ArrayList@11052} size = 1 this = {MainActivity@11051} view = {AppCompatButton@11053} "android.support.v7.widget.AppCompatButton {1bc3388 VFED..C.. ...P.... 720,0-1028,168 #7f070019 app:id/add}" person = {Person@11054} “哈利波特”适配器 = {ArrayAdapter@11055}人 = {ArrayList@11052} 大小 = 1
  • 请编辑问题并将其添加到那里。我无法在 cmets 中读取它。
  • 我的错,对不起。我是 Java 和 Android 工作室的绝对新手。我已经编辑了原始问题,但不确定我得到了正确的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多