-1

I am learning Android right now and working on the list view to display multiples items for the list. It might be stupid question but I have to ask to find down the reason behind the scenes ( if there are any )

Below are 2 ways to populate the data to the list view

way 1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ListView    moblieListView                      =   getListView();
    MobileListAdapter   mobileListAdapter           =   new MobileListAdapter(getApplicationContext(), mobileList);
    moblieListView.setAdapter(mobileListAdapter);

    moblieListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedValue = (String)parent.getItemAtPosition(position);
            Toast.makeText(getApplicationContext(), selectedValue, Toast.LENGTH_SHORT).show();

        }       
    }); 
}

way 2

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MobileListAdapter   mobileListAdapter           =   new MobileListAdapter(getApplicationContext(), mobileList);
    setListAdapter(mobileListAdapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    String str = l.getItemAtPosition(position).toString();
    Toast.makeText(MobileActivity.this, str, Toast.LENGTH_SHORT).show();
}

What I am seeing right now is way1 is having an object of ListView, but way2. Another point is onItemClick is located in onCreate in way1 , however, onItemClick is separated from onCreate in way2.

Question Are there any particular reasons when we should use way1 over way2 or vice versa. please help if you have any ideas about these.

tranvutuan
  • 6,089
  • 8
  • 47
  • 83
  • i dont know why I am getting a down vote. If you can, just give me an explanation and then do a down vote then.. – tranvutuan Apr 17 '13 at 19:20

1 Answers1

1

Both are fine and people use both of the methods.

However, I prefer the first method.

Reasons :

  1. You don't end up with a class that implements so many interfaces.

  2. In case of multiple views(like onClickListener), you don't have to check which of the views was the one that caused the event.

  3. You are staying in the same area that you've written about the view to be handled, making it less annoying to scroll up and down.

  4. It makes more sense, as the activity is not the one being able to have some item clicked on it, it's the listView the one that supports it.

  5. You know exactly which view is attached to which listener.

  6. Less mess in case of multiple views (what would you do in case you have 2 listViews ?) .

  7. Very little methods are supported in xml for the second method. for views, I am only familiar with onClick . So the second method loses the whole point anyway.

android developer
  • 114,585
  • 152
  • 739
  • 1,270