0

After each notifyDataSetChanged() the getView() method of my custom adapter gets called multiple time and I can't understand why. The main problem is that there's a network request inside the getView method (done on a different thread) that retrieves some item description and I get spammed of requests for this.

Here's the code

@Override
public View getView(int position, View view, ViewGroup parent) {
    Log.w("", "getView position: " + position);
    LayoutInflater inflater = context.getLayoutInflater();
    String flags = Singleton.getInstance().getMyZones().get(position).getParsedFlags();
    View rowView;

    if (Singleton.getInstance().getFiltertype() == constants.FILTER_ALL || (Singleton.getInstance().getFiltertype() == constants.FILTER_OPEN && flags.charAt(0) == '1') || (Singleton.getInstance().getFiltertype() == constants.FILTER_CLOSED && flags.charAt(2) == '1')) {
        Database db = new Database(context);
        Description description = db.getDescriptionByItemId(Integer.parseInt(Singleton.getInstance().getMyZones().get(position).getId()), plant.getIdplant(), userId, 1);
        String descriptionText;

        rowView = inflater.inflate(R.layout.zoneslist, null, true);

        if (description != null) {
            descriptionText = description.getDescription();
            if (descriptionText.isEmpty()) {
                descriptionText = context.getString(R.string.base_zone) + " " + Singleton.getInstance().getMyZones().get(position).getId();
            }
        } else {
            descriptionText = context.getString(R.string.base_zone) + " " + Singleton.getInstance().getMyZones().get(position).getId();
            Singleton.getInstance().getComunicator().addLowPriorityJob(new GetDescriptionJob(Singleton.getInstance().getMyZones().get(position).getId(), Integer.toString(1), context, Integer.toString(plant.getIdplant()), userId, anHanlder));
        }

        if (position != 0){
            rowView.findViewById(R.id.SplitLine_hor2).setVisibility(View.GONE);
        }

        TextView zoneName = (TextView) rowView.findViewById(R.id.zoneName);
        TextView zoneStatus = (TextView) rowView.findViewById(R.id.zoneStatus);
        ImageView zoneSymbol = (ImageView) rowView.findViewById(R.id.zoneSymbol);

        zoneName.setText(descriptionText);

        if (flags.charAt(7) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_generic_alarm);
            zoneStatus.setText(context.getString(R.string.zone_alarm));
            zoneStatus.setTextColor(Color.RED);
            zoneName.setTextColor(Color.RED);
        } else if (flags.charAt(1) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_generic_tamper);
            zoneStatus.setText(context.getString(R.string.zone_tamper));
            zoneStatus.setTextColor(Color.RED);
            zoneName.setTextColor(Color.RED);
        } else if (flags.charAt(2) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_contact_excluded);
            zoneStatus.setText(context.getString(R.string.zone_exclude));
            zoneStatus.setTextColor(Color.BLACK);
            zoneName.setTextColor(Color.BLACK);
        } else if (flags.charAt(4) == '1' || flags.charAt(5) == '1' || flags.charAt(6) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_generic_failure);
            zoneStatus.setText(context.getString(R.string.zone_warning));
            zoneStatus.setTextColor(Color.BLACK);
            zoneName.setTextColor(Color.BLACK);
        } else if (flags.charAt(3) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_system_armed);
            zoneStatus.setText(context.getString(R.string.zone_active));
            zoneStatus.setTextColor(Color.BLACK);
            zoneName.setTextColor(Color.BLACK);
        } else if (flags.charAt(0) == '1') {
            zoneSymbol.setImageResource(R.drawable.icon_contact_open);
            zoneStatus.setText(context.getString(R.string.zone_open));
            zoneStatus.setTextColor(Color.RED);
            zoneName.setTextColor(Color.RED);
        } else {
            zoneSymbol.setImageResource(R.drawable.icon_contact_close);
            zoneStatus.setText(context.getString(R.string.zone_close));
            zoneStatus.setTextColor(Color.BLACK);
            zoneName.setTextColor(Color.BLACK);
        }
    } else {
        rowView = inflater.inflate(R.layout.null_item, null, true);
    }

    return rowView;
}

And this is the logcat:

10-21 09:53:10.729  16012-16139/com.mydomain.myapp W/NOTICE﹕ GetZones request done
10-21 09:53:10.729  16012-16012/com.mydomain.myapp W/﹕ getView position: 0
10-21 09:53:10.759  16012-16012/com.mydomain.myapp W/﹕ getView position: 1
10-21 09:53:10.769  16012-16012/com.mydomain.myapp W/﹕ getView position: 2
10-21 09:53:10.789  16012-16012/com.mydomain.myapp W/﹕ getView position: 3
10-21 09:53:10.809  16012-16012/com.mydomain.myapp W/﹕ getView position: 4
10-21 09:53:10.829  16012-16012/com.mydomain.myapp W/﹕ getView position: 5
10-21 09:53:10.839  16012-16012/com.mydomain.myapp W/﹕ getView position: 6
10-21 09:53:10.859  16012-16012/com.mydomain.myapp W/﹕ getView position: 0
10-21 09:53:10.879  16012-16012/com.mydomain.myapp W/﹕ getView position: 1
10-21 09:53:10.889  16012-16012/com.mydomain.myapp W/﹕ getView position: 2
10-21 09:53:10.909  16012-16012/com.mydomain.myapp W/﹕ getView position: 3
10-21 09:53:10.929  16012-16012/com.mydomain.myapp W/﹕ getView position: 4
10-21 09:53:10.940  16012-16012/com.mydomain.myapp W/﹕ getView position: 5
10-21 09:53:10.960  16012-16012/com.mydomain.myapp W/﹕ getView position: 6

EDIT: Almost forgot, my ListView has already

android:layout_height="fill_parent"

which was one of the most used advice i found on StackOverflow. Tried match_parent too but without any success.

Cusy
  • 233
  • 1
  • 3
  • 11
  • Question is not a duplicate. Already tried the solution proposed (putting fill/match_parent as ListView layout_height) but without success. Please remove the mark. – Cusy Oct 21 '14 at 08:21
  • you must provide a mechanism to cancel your network request and also preventing to call it multiple times if it has already been called. – mmlooloo Oct 21 '14 at 08:28
  • That's not a solution to my question. It still get renderd multiple times which is a waste of resoruces. – Cusy Oct 21 '14 at 08:30
  • GetView will be called for each row in your listview, so it's not a good idea making requests without using a cache to store your data in order to not getting the same thing more than once – Juan Aguilar Guisado Oct 21 '14 at 09:09

0 Answers0