在Android中运行onResume()时更新列表视图

时间:2014-02-27 09:36:05

标签: android listview

我创建了一个listview,从Android的地址簿获取所有联系人。我尝试通过onResume()更新listview,但它不起作用。

这是我的代码:

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!

2 个答案:

答案 0 :(得分:0)

尝试替换你的updateData():

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

答案 1 :(得分:0)

您可能正在引用旧的ListView(lv),它不是您在恢复活动时看到的新ListView。尝试使用onResume()中的Activity.findViewById(R.id.xxxx)再次获取lv的引用。

您还可以尝试重新使用旧适配器,而不是创建一个全新的适配器(通过清除它并更新它的内容)。如果您正在重用旧适配器,请确保使用ListView.setAdapter(ma)将旧适配器提供给新的ListView。

你也应该让MyAdapter.getItemId(int position)返回位置而不是0。