为了账号安全,请及时绑定邮箱和手机立即绑定

使用 listView 和适配器显示对象

使用 listView 和适配器显示对象

qq_笑_17 2021-09-26 16:52:20
我正在编写一个简单的联系人列表应用程序,它允许用户输入姓名和姓氏,将这些项目存储在列表中,并将它们显示为 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<>();@Overrideprotected 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();}
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

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


如果是这种情况,那么你应该改变people = Arrays.asList(person);由people.add(person);现在它总是与一个人的一个新的列表替换现有列表的方式。


此外,我没有看到该add()方法接收Viewas 参数的任何理由。除非您打算做其他事情,否则可以将其删除。


最后,在您发布的代码中,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:

//img1.sycdn.imooc.com//615034eb00013b0403870075.jpg

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

查看完整回答
反对 回复 2021-09-26
  • 1 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信