Mr.Mojo Mr.Mojo - 2 months ago 10x
Android Question

Android: Select first item by default in ListView in Master Detail template

I program a RSS reader with Master Detail flow with a list view on the left, and a detail view on the right.

When the screen is created the right side is empty and no item on the left is selected. How can I load the first item by default?

I found this post which is similar to my problem: Select the first item by default in a Master Detail flow

The code in my ListFragment looks like this:

public void onActivityCreated(Bundle savedInstanceState) {

// Set a listener to be invoked when the list should be refreshed.
listView = (PullToRefreshListView) getListView();

listView.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
//refresh list


// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);

//create button, on click go to website
final Button btnAddMore = new Button(getActivity());
btnAddMore.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// go to website on click
String url = "";
Intent web = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//add button at the end of the listview

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {

//select the first item in the listview by default
.getAdapter().getView(0, view, null), 0, 0);

public void onCreate(Bundle savedInstanceState) {

Log.d(TAG, "Hallo in ItemListFragment");

// show ActionBar

// get reference to activity
myApp = getActivity().getApplication();

// check if intent from ItemListActivity is null
Bundle be = getActivity().getIntent().getExtras();
if (be == null) {
// if null read local feed
feed = ReadFeed(fileName);
Log.d(TAG, "Lese Feed lokal :" + feed);
} else {
// else get extras from the intent
feed = (RSSFeed) getActivity().getIntent().getExtras().get("feed");
"Intent von ItemListActivity an ItemListFragment vorhanden");

But I receive this error:

01-19 06:39:14.723: E/AndroidRuntime(1921): Caused by: java.lang.NullPointerException
01-19 06:39:14.723: E/AndroidRuntime(1921): at ItemListFragment.onViewCreated(

in the line


Please help me!


public class CustomListAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public RSSFeed _feed;
public Date pDate;

public CustomListAdapter(Activity activity, RSSFeed feed) {

_feed = feed;

layoutInflater = (LayoutInflater) activity
imageLoader = new ImageLoader(activity.getApplicationContext());

public void setNewFeed(RSSFeed feed) {
// set new Feed list, after refresh
_feed = feed;

public int getCount() {
// Set the total list item count
return _feed.getItemCount();

public Object getItem(int position) {
return position;

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, ViewGroup parent) {

// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);

// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(;
TextView tvTitle = (TextView) listItem.findViewById(;
TextView tvDate = (TextView) listItem.findViewById(;
TextView comment_bubble = (TextView) listItem.findViewById(;

// Set the views in the layout
imageLoader.DisplayImage(_feed.getItem(pos).getImage(), iv);

// calculate the time difference to the actual system time
String pubDate = _feed.getItem(pos).getDate();
SimpleDateFormat df = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss",Locale.ENGLISH);
try {
try {
pDate = df.parse(pubDate);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
pubDate = "Vor "
+ DateUtils.getDateDifference(pDate);
} catch (ParseException e) {
Log.e("DATE PARSING", "Error parsing date..");

//set time difference

//set comment in bubble

return listItem;


I solved it this way inside my ListFragment:

    public void onStart() {

        //set first item activated by default
        onListItemClick(listView, getView(), 1, 0);
 * Turns on activate-on-click mode. When this mode is on, list items will be
 * given the 'activated' state when touched.
public void setActivateOnItemClick(boolean activateOnItemClick) {
    // When setting CHOICE_MODE_SINGLE, ListView will automatically
    // give items the 'activated' state when touched.
    listView.setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
            : ListView.CHOICE_MODE_NONE);