I am writing a checkbook Android app that displays a list of bank accounts inside of a Navigation Drawer. The main screen of the application displays the basic account information (name, balance, etc).
I was able to implement the onClick method so that when an account is selected the main screen is updated, but I would like to implement this in a way that the first item in the ListView is selected when the application opens.
In other words, when I start the application I should see checking account information, but all that I see at the moment are default 0s, because no Intent was passed to the Activity when it started. I have tried to programmatically select the first item in the onCreate method of the main activity, but that did not work.
EDIT
Here is my code for the onCreate of the main activity:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerListView = (ListView) rootView.findViewById(R.id.listview_account);
// Get the Cursor that will be used to load accounts
Cursor accounts = dataSource.getAccounts();
// Make adapter.
mAccountAdapter = new AccountAdapter(getActivity(), accounts, 0);
mDrawerListView.setAdapter(mAccountAdapter);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
HeaderViewListAdapter headerViewAdapter = (HeaderViewListAdapter) adapterView.getAdapter();
AccountAdapter adapter = (AccountAdapter) headerViewAdapter.getWrappedAdapter();
Cursor cursor = adapter.getCursor();
if(cursor != null && cursor.moveToPosition(i)){
// Adjust main page
}
}
});
return rootView;
}
I tried to programatically select the first item after calling the setUp
method, but that did not work. Should this be handled in the main activity or the fragment?