2015-07-12 51 views
0

代码的目的是使联系人管理器应用程序一切工作倒是接触时,它不被显示在列表中,当我们单击列表选项卡。不完整或不理想的输出

代码:

package com.example.tanmay.myapplication2; 

public class MainActivity extends ActionBarActivity { 

EditText nameTxt,phoneTxt,emailTxt,addressTxt; 
List<Contact> Contacts=new ArrayList<Contact>(); 
ListView contactListView; 

@Override 
protected void onCreate(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) findViewById(R.id.tabHost); 

    tabHost.setup(); 

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("creator"); 
    tabSpec.setContent(R.id.tabCreator); 
    tabSpec.setIndicator("creator"); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("list"); 
    tabSpec.setContent(R.id.tabContactList); 
    tabSpec.setIndicator("List"); 
    tabHost.addTab(tabSpec); 


    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()); 
      populateList(); 
      Toast.makeText(getApplicationContext(),nameTxt.getText().toString() +"has been added to your contacts!",Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    nameTxt.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      addBtn.setEnabled(!nameTxt.getText().toString().trim().isEmpty()); 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
} 

private void populateList(){ 
    ArrayAdapter<Contact> adapter = new ContactListAdapter(); 
    contactListView.setAdapter(adapter); 
} 

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

} 

private class ContactListAdapter extends com.example.tanmay.myapplication2.ContactListAdapter { 
    public ContactListAdapter(){ 
     super(MainActivity.this,R.layout.listview_item, Contacts); 
    } 

    @Override 
    public View getView(int position,View view,ViewGroup parent){ 
     if(view==null) 
      view = getLayoutInflater().inflate(R.layout.listview_item,parent,false); 

     Contact currentContact = Contacts.get(position); 

     TextView name=(TextView) view.findViewById(R.id.contactName); 
     name.setText(currentContact.getName()); 
     TextView phone=(TextView) view.findViewById(R.id.phoneNumber); 
     phone.setText(currentContact.getPhone()); 
     TextView email=(TextView) view.findViewById(R.id.emailAddress); 
     email.setText(currentContact.getEmail()); 
     TextView address=(TextView) view.findViewById(R.id.cAddress); 
     address.setText(currentContact.getAddress()); 

     return view; 
    } 



    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 
} 



} 

回答

0

您必须通知您的适配器,您的数据集已通过添加联系人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(); 
    } 
}); 
+0

我如何通知)我的适配器,我的数据集已通过调用notifyDataSetChanged(更新(调用它在那里)和我如何添加联系人到ArrayList的 – Andy

+0

其示值误差私人不能被用作私人ContactListAdapter mAdapter其显示非法启动表达错误 – Andy

+0

忘记了上述意见 – Andy