使用 listView 和适配器显示对象

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

}


qq_笑_17
浏览 150回答 1
1回答

紫衣仙女

我猜的add()方法应该取的名字和姓氏从edit texts,创建一个新的Person,并把它添加到列表中people。如果是这种情况,那么你应该改变people = Arrays.asList(person);由people.add(person);现在它总是与一个人的一个新的列表替换现有列表的方式。此外,我没有看到该add()方法接收Viewas 参数的任何理由。除非您打算做其他事情,否则可以将其删除。最后,在您发布的代码中,add()没有在任何地方被调用。也许它应该OnClickListener在布局上的按钮内调用。编辑我您使用 Person 类的方式不应该有静态成员。静态成员在所有实例之间共享。因此,更改名称(如果它是静态的)将更改所有 Person 实例的名称。public class Person {&nbsp; &nbsp; private String firstname;&nbsp; &nbsp; private String lastname;&nbsp; &nbsp; public Person(String firstname, String lastname) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstname = firstname;&nbsp; &nbsp; &nbsp; &nbsp; this.lastname = lastname;&nbsp; &nbsp; }&nbsp; &nbsp; public static String getFirstname() {&nbsp; &nbsp; &nbsp; &nbsp; return firstname;&nbsp; &nbsp; }&nbsp; &nbsp; public static String getLastname() {&nbsp; &nbsp; &nbsp; &nbsp; return lastname;&nbsp; &nbsp; }&nbsp; &nbsp; public String toString(){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; getFirstname()+ " " + getLastname();&nbsp; &nbsp; }}对于崩溃,请检查左侧底部的 Logcat:并寻找Exception带有stack trace.堆栈跟踪具有直到抛出异常的所有调用层次结构。您需要将其添加到问题中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java