ListView中adapter加载数据的两种方式

因为想要把查询数据的方法封到一个类里面,尝试了不同的往adapter中加载数据的方法:
1.在初始化时只配置适配器,然后在后面的方法中通过调用查询数据的方法往适配器中加载数据,并通知适配器更新数据。(这是以前一直用的方法)

PhoneNumList_Adapter adapter;
List phoneNumList = new ArrayList<>();
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_read_phone_num, container, false);
       adapter = new PhoneNumList_Adapter(getContext(), R.layout.phone_num_list_item, phoneNumList);
       listView = (ListView) view.findViewById(R.id.PhoneNum_listView);
       listView.setAdapter(adapter);
       return view;
  }
  @Override
  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

          @Override
          public void onItemClick(AdapterView adapterView, View view, int position, long id) {
              if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                  ActivityCompat.requestPermissions((Activity) getContext(), new String[]{Manifest.permission.CALL_PHONE}, 2);
              } else {
                  clickPosition = position;
                  call(clickPosition);
              }
          }
      });

      if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions((Activity) getContext(), new String[]{Manifest.permission.READ_CONTACTS}, 1);
      } else {
          readContacts();
      }
  }
  private void readContacts() {
    Cursor cursor = null;
      try {
        cursor = getActivity().getContentResolver().query(ContactsContract.
          CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
              if (cursor != null) {
                  while (cursor.moveToNext()) {
                      String displayName = cursor.getString(cursor.getColumnIndex
                              (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                      String number = cursor.getString(cursor.getColumnIndex
                              (ContactsContract.CommonDataKinds.Phone.NUMBER));
                      PhoneNumber person = new PhoneNumber(displayName,number);
                      phoneNumList.add(person);
                  }
                  adapter.notifyDataSetChanged();
              }
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              if (cursor != null) {
                  cursor.close();
              }
          }
      }

2.在adapter初始化之前就通过调用获取数据的方法将数据存入到一个list中,使得adapter初始化时传入的参数里面已经携带有了数据。
查询数据的类:

public class Read {
    public static List readContacts(Context context) {
        Cursor cursor = null;
        List phoneNumList = new ArrayList<>();
        try {
            cursor = context.getContentResolver().query(ContactsContract.
                    CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

            if (cursor != null) {
                while (cursor.moveToNext()) {
                    String displayName = cursor.getString(cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER));
                    PhoneNumber person = new PhoneNumber(displayName,number);
                    phoneNumList.add(person);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return phoneNumList;
    }
}

主函数:

PhoneNumList_Adapter adapter;
List phoneNumList = new ArrayList<>();
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_read_phone_num, container, false);
       phoneNumList = Read.readContacts(getContext());//因为这个我写的是fragment,不能用this来获取context
       adapter = new PhoneNumList_Adapter(getContext(), R.layout.phone_num_list_item, phoneNumList);
       listView = (ListView) view.findViewById(R.id.PhoneNum_listView);
       listView.setAdapter(adapter);
       return view;
  }

你可能感兴趣的:(private)