Android 不完整或不是理想的输出

Android 不完整或不是理想的输出,android,Android,代码是关于使联系人管理器应用程序一切正常,但当添加联系人时,当我们单击列表选项卡时,它不会显示在列表中 代码: package com.example.tanmay.myapplication2; 公共类MainActivity扩展了ActionBarActivity{ EditText-nameTxt、phoneTxt、emailTxt、addressTxt; 列表联系人=新建ArrayList(); ListView联系人ListView; @凌驾 创建时受保护的void(Bundle sa

代码是关于使联系人管理器应用程序一切正常,但当添加联系人时,当我们单击列表选项卡时,它不会显示在列表中

代码:

package com.example.tanmay.myapplication2;
公共类MainActivity扩展了ActionBarActivity{
EditText-nameTxt、phoneTxt、emailTxt、addressTxt;
列表联系人=新建ArrayList();
ListView联系人ListView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameTxt=(EditText)findViewById(R.id.txtName);
phoneTxt=(EditText)findViewById(R.id.txtPhone);
emailTxt=(EditText)findViewById(R.id.txtEmail);
addressTxt=(EditText)findViewById(R.id.txtAddress);
contactListView=(ListView)findViewById(R.id.ListView);
TabHost TabHost=(TabHost)findviewbyd(R.id.TabHost);
tabHost.setup();
TabHost.TabSpec TabSpec=TabHost.newTabSpec(“创建者”);
tabSpec.setContent(R.id.tabCreator);
tabSpec.setIndicator(“创建者”);
tabHost.addTab(tabSpec);
tabSpec=tabHost.newTabSpec(“列表”);
tabSpec.setContent(R.id.tabContactList);
tabSpec.setIndicator(“列表”);
tabHost.addTab(tabSpec);
最终按钮addBtn=(按钮)findviewbyd(R.id.btnAdd);
addBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
addContact(nameTxt.getText().toString()、phoneTxt.getText().toString()、emailTxt.getText().toString()、addressTxt.getText().toString());
大众主义者();
Toast.makeText(getApplicationContext(),nameTxt.getText().toString()+“已添加到您的联系人中!”,Toast.LENGTH_SHORT.show();
}
});
nameTxt.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty());
}
@凌驾
公共无效后文本已更改(可编辑){
}
});
}
私有void populateList(){
ArrayAdapter=新的ContactListAdapter();
contactListView.setAdapter(适配器);
}
私人void addContact(字符串名称、字符串电话、字符串电子邮件、字符串地址){
联系人。添加(新联系人(姓名、电话、电子邮件、地址));
}
私有类ContactListAdapter扩展com.example.tanmay.myapplication2.ContactListAdapter{
公共联系人列表适配器(){
super(MainActivity.this,R.layout.listview_项,Contacts);
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
如果(视图==null)
view=GetLayoutFlater().充气(R.layout.listview\u项,父项,false);
触点电流触点=触点。获取(位置);
TextView name=(TextView)view.findViewById(R.id.contactName);
name.setText(currentContact.getName());
TextView phone=(TextView)view.findViewById(R.id.phoneNumber);
setText(currentContact.getPhone());
TextView email=(TextView)view.findViewById(R.id.emailAddress);
setText(currentContact.getEmail());
TextView地址=(TextView)view.findViewById(R.id.cadAddress);
address.setText(currentContact.getAddress());
返回视图;
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
}
}

将联系人POJO添加到ArrayList后,必须通过调用
notifyDataSetChanged()
通知适配器数据集已更新

为此,您需要保留适配器的引用

onClick()
回调中调用
addContact()
后,不要调用
populateList()
,因为它会完全重置您不想要的列表

以下是您需要的:

private ContactListAdapter mAdapter;

private void populateList() {
    mAdapter = new ContactListAdapter();
    contactListView.setAdapter(mAdapter);
}

private void addContact(String name,String phone,String email,String address) {
    Contacts.add(new Contact(name,phone,email,address));
    mAdapter.notifyDataSetChanged();

}

final Button addBtn=(Button) findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        addContact(nameTxt.getText().toString(),phoneTxt.getText().toString(),emailTxt.getText().toString(),addressTxt.getText().toString());
        Toast.makeText(getApplicationContext(),nameTxt.getText().toString() +"has been added to your contacts!",Toast.LENGTH_SHORT).show();
    }
});

如何通过调用notifyDataSetChanged()(在何处调用)通知适配器我的数据集已更新如何将联系人添加到ArrayListS显示private不能用作private ContactListAdapter mAdapter的错误以及显示非法的开始表达式错误忘记上面的注释正在运行,但当我单击addcontacts按钮并显示消息myapplication2 stops error时,它会停止如果您得到NPE,很可能是因为您切勿通过在onCreate()中调用populateList()来设置适配器。因此,您尝试在空引用上调用notifyDataSetChanged。
private ContactListAdapter mAdapter;

private void populateList() {
    mAdapter = new ContactListAdapter();
    contactListView.setAdapter(mAdapter);
}

private void addContact(String name,String phone,String email,String address) {
    Contacts.add(new Contact(name,phone,email,address));
    mAdapter.notifyDataSetChanged();

}

final Button addBtn=(Button) findViewById(R.id.btnAdd);
addBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        addContact(nameTxt.getText().toString(),phoneTxt.getText().toString(),emailTxt.getText().toString(),addressTxt.getText().toString());
        Toast.makeText(getApplicationContext(),nameTxt.getText().toString() +"has been added to your contacts!",Toast.LENGTH_SHORT).show();
    }
});