第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 listView 和適配器顯示對象

使用 listView 和適配器顯示對象

qq_笑_17 2021-09-26 16:52:20
我正在編寫一個簡單的聯(lián)系人列表應(yīng)用程序,它允許用戶輸入姓名和姓氏,將這些項(xiàng)目存儲在列表中,并將它們顯示為 editText 字段下方的列表。我創(chuàng)建了一個具有名字和姓氏兩個字段的對象,并嘗試將對象添加到列表以顯示在按鈕下方。但是,屏幕上什么也沒有出現(xiàn)。調(diào)試消息顯示對象已成功創(chuàng)建并添加到列表中,但我嘗試顯示對象字段值的方式一定有問題。如果有人能告訴我我在這里做錯了什么,我將不勝感激。這是布局 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貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個贊

我猜的add()方法應(yīng)該取的名字和姓氏從edit texts,創(chuàng)建一個新的Person,并把它添加到列表中people。


如果是這種情況,那么你應(yīng)該改變people = Arrays.asList(person);由people.add(person);現(xiàn)在它總是與一個人的一個新的列表替換現(xiàn)有列表的方式。


此外,我沒有看到該add()方法接收Viewas 參數(shù)的任何理由。除非您打算做其他事情,否則可以將其刪除。


最后,在您發(fā)布的代碼中,add()沒有在任何地方被調(diào)用。也許它應(yīng)該OnClickListener在布局上的按鈕內(nèi)調(diào)用。


編輯我


您使用 Person 類的方式不應(yīng)該有靜態(tài)成員。靜態(tài)成員在所有實(shí)例之間共享。因此,更改名稱(如果它是靜態(tài)的)將更改所有 Person 實(shí)例的名稱。


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

    }

}

對于崩潰,請檢查左側(cè)底部的 Logcat:

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

并尋找Exception帶有stack trace.
堆棧跟蹤具有直到拋出異常的所有調(diào)用層次結(jié)構(gòu)。您需要將其添加到問題中。

查看完整回答
反對 回復(fù) 2021-09-26
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號