getView多次调用

时间:2014-10-21 08:08:05

标签: android android-listview getview

在每个notifyDataSetChanged()之后,我的自定义适配器的getView()方法被多次调用,我无法理解为什么。主要的问题是getView方法中有一个网络请求(在不同的线程上完成),它检索一些项目描述,并且我收到了对此请求的垃圾邮件。

这是代码

@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;
}

这是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
编辑:差点忘了,我的ListView已经

android:layout_height="fill_parent"

这是我在StackOverflow上找到的最常用的建议之一。尝试了match_parent但没有任何成功。

0 个答案:

没有答案