-1

In custom adapters click event handling, which is better (considering performance and memory) , 1)Defining click handler at ListView level. 2) Defining a click handler inside custom adapter.

Will defining click handler at Custom Adapter level increase the memory consumption?. Below is some sample pseudo code.

//Defining click handler at list level

layoutNotificationViewHolder.mNotificationListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final SampleItem selectedItem = itemList.get(position);
            Intent itemInfoIntent = new Intent(context, itemMoreInfoActivity.class);
            itemInfoIntent.putExtra("selected_item", selectedItem);
            context.startActivity(itemInfoIntent);
        }
    });

//Defining click handler inside adapter.

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

 final SampleItem selectedItem = itemList.get(position);
 convertView.mCameraIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemInfoIntent = new Intent(context, itemMoreInfoActivity.class);
                itemInfoIntent.putExtra("selected_item", selectedItem);
                context.startActivity(itemInfoIntent);
            }
        });

}
Shiva
  • 543
  • 1
  • 6
  • 20
  • `considering performance and memory` why do you think writing code at some place will effect the performance? I prefer writing on click code in adapter as it's easy to read and maintain.As of my knowledge there won't be any effect on performance until the business logic you are defining is not having complexity(loops) and effecting performance in turn. – Anmol Jan 24 '19 at 06:53

1 Answers1

0

First, List view is depracated, use RecyclerView instead. For your question, if the handler of the clicks is the adapter (ex 1) so no new objects will be created which is good for memory, and if you will set it like in ex2 - new object will be created each time getView function will be called which less good. But in general it is not so expensive so both examples are fine.

omri tal
  • 98
  • 8
  • This is some legacy code, I don't have an option to get rid of ListView at this point of time. Are you sure that new object will be created each time getView function gets called? I was thinking, since the object type is final and it can't be modified, the object reference should simply hold a pointer/reference to the object inside the ListView Array. – Shiva Jan 23 '19 at 16:37
  • `no new objects will be created which is good for memory` where is new object created or not created everytime?? – Anmol Jan 24 '19 at 06:56