2015-07-12 83 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