2014-02-27 47 views
0

我创建一个listview从Android地址簿中获取所有联系人。我尝试通过onResume()更新listview,但它不起作用。在Android中运行onResume()时更新列表视图

这里是我的代码:

public class Display extends Activity implements OnItemClickListener{ 

    List<String> name1 = new ArrayList<String>(); 
    List<String> phno1 = new ArrayList<String>(); 
    List<String> email1 = new ArrayList<String>(); 
    MyAdapter ma ; 
    ListView lv; 
    int countContact; 
    TextView textViewManyCount; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display); 

     getAllContacts(this.getContentResolver()); 
     lv= (ListView) findViewById(R.id.lv); 
      ma = new MyAdapter(); 
      lv.setAdapter(ma); 
      lv.setOnItemClickListener(this); 
      lv.setItemsCanFocus(false); 
      lv.setTextFilterEnabled(true); 
      textViewManyCount = (TextView) findViewById(R.id.textViewManyCount); 
    } 
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
    } 

    public void getAllContacts(ContentResolver cr) { 

     Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
     countContact = phones.getCount(); 

     while (phones.moveToNext()) 
     { 
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
      System.out.println(".................."+phoneNumber); 
      name1.add(name); 
      phno1.add(phoneNumber); 
      email1.add(email); 
     } 

     phones.close(); 
    } 
    class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener 
    { private SparseBooleanArray mCheckStates; 
     LayoutInflater mInflater; 
     TextView textViewName,textViewPhone,textViewEmail; 
     MyAdapter() 
     { 
      mCheckStates = new SparseBooleanArray(name1.size()); 
      mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 
     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return name1.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 

      return 0; 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View vi=convertView; 
      if(convertView==null) 
      vi = mInflater.inflate(R.layout.list_user, null); 
      textViewName = (TextView) vi.findViewById(R.id.textViewName); 
      textViewPhone = (TextView) vi.findViewById(R.id.textViewPhone); 
      textViewEmail = (TextView) vi.findViewById(R.id.textViewEmail); 


      textViewName.setText("Name :"+ name1.get(position)); 
      textViewPhone.setText("Phone No :"+ phno1.get(position)); 
      textViewEmail.setText("Email :"+ email1.get(position)); 
      textViewManyCount.setText("Contacts: "+countContact); 

      return vi; 
     } 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
       boolean isChecked) { 
      // TODO Auto-generated method stub 

     } 
    } 

当我运行的应用程序,它加载所有接触,但是当我点击主页按钮,更改地址簿中的一些信息然后点击应用程序图标再次打开应用程序。而onResume()方法不起作用。

这里我的onResume()

@Override 
     protected void onResume() 
     { 
      super.onResume(); 

      if (lv != null) 
      { 
       updateData(); 
      } 
     } 

     private void updateData() 
     { 
      getAllContacts(this.getContentResolver()); 
      ma = new MyAdapter(); 
      lv.setAdapter(ma); 
      ma.notifyDataSetChanged(); 
     } 

所以一些身体帮我! PLZ!

+0

尝试,因为'ma.clear(); getAllContacts(this.getContentResolver()); lv.setAdapter(MA); ma.notifyDataSetChanged();' –

+0

@ρяσѕρєяK什么是clear();我没有在MyAdapter – binhnt

+0

中添加'public clear(){name1.clear(); phno1.clear(); email1.clear();}'MyAdapter然后尝试 –

回答

0

尝试通过更换你的了updateData():

 private void updateData() 
     { 
      getAllContacts(this.getContentResolver()); 
      ma.notifyDataSetChanged(); 
     } 
+0

它不工作。其他什么?:( – binhnt

+0

你只是尝试更新你的listview数据,只需点击按钮并检查你是否得到更新数据在ListView中? –

0

你可能会引用一个旧的ListView(LV),是不是你看到的时候你恢复你的活动新的ListView。尝试使用onResume()中的Activity.findViewById(R.id.xxxx)再次获取对lv的引用。

您也可以尝试重新使用旧的适配器,而不是创建一个全新的适配器(通过清除它并更新它的内容)。如果您重新使用旧的适配器,只要确保您使用ListView.setAdapter(ma)将旧适配器添加到新的ListView。

你也应该有MyAdapter.getItemId(INT位置)返回前的位置而不是0