0

在每个 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 但没有任何成功。

4

0 回答 0