I want to list the most recently messaged contacts in a navigation drawer from most recent at the top, however mDrawerListView (where navigation drawer's string are created) is in NavigationDrawerFragment.java and the function to list names can't be used since getContentResolver() and getApplicationContext() are undefined. I am also unsure how I would put the items in the names list as mDrawerListView's strings.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
getAllSms();
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
public void getAllSms() {
Uri message = Uri.parse("content://sms/");
ContentResolver cr = getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
int totalSMS = c.getCount();
List<String> names = new ArrayList<String>();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
names.add(getContactName(
getApplicationContext(),
c.getString(c
.getColumnIndexOrThrow("address"))));
c.moveToNext();
}
}
c.close();
}