列表视图在更新适配器 - android 后显示较少的项目



我正在使用自定义适配器将一些数据从 JSON 拉取到我的列表视图:

adapter = new ActorAdapter(this, R.layout.data_item, actorsList);

JSON 包含 5 个项目。一切都很好,所有项目都显示出来,但是...由于我还使用位置服务,因此我需要更新列表视图(距离等(。

这在这里发生:

       private void updateUI(Location loc) {
        double Act1=loc.getLatitude();
        double Act2=loc.getLongitude();
        ListView lv = findViewById(R.id.listView1);
        lv.setAdapter(adapter);
adapter.clear();adapter.notifyDataSetChanged();
        new GetContacts().execute();}

问题是,每次更新后,我的 ListView 都有更多次相同的数据 - 重复。正如你在上面看到的,我试图使用adapter.clear();adapter.notifyDataSetChanged();

但是,这几乎可以正常工作,不再重复 - 但是每次更新后,ListView 始终仅显示 4 个项目而不是 5 个项目。

因此,第一次 ListView 加载 5 个项目 - 好的,但在下一次和每次更新之后,它只显示 4 个项目。这很奇怪,找不到问题,为什么会这样...

附加我的获取联系人异步任务:

    class GetContacts extends AsyncTask<String, Void, Boolean> {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Okoli.this);
        dialog.setMessage("Loading data");
        dialog.setTitle("Connecting");
        dialog.show();
        dialog.setCancelable(false);
    }
    @Override
    protected Boolean doInBackground(String... args) {
        HttpHandler sh = new HttpHandler();
        String url = "androidnews.json";
        String jsonStr = sh.makeServiceCall(url);Double myDistx;
        if (jsonStr != null) {
            try {JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray actors = jsonObj.getJSONArray("result");
                for (int i = 0; i < actors.length(); i++) {
                    JSONObject c = actors.getJSONObject(i);
                    Actors actor = new Actors();
                    double g1 = Double.parseDouble(c.getString("gps1"));
                    double g2 = Double.parseDouble(c.getString("gps2"));
                    double Act1=loc.getLatitude();
                    double Act2=loc.getLongitude();
                    myDistx = meterDistanceBetweenPoints(g1,g2,Act1,Act2);
                    actor.setNazov(c.getString("name"));
                    actor.setPerex(c.getString("perex"));
                    actor.setPlace(c.getString("place"));
                    actor.setGps1(c.getString("gps1"));
                    actor.setGps2(c.getString("gps2"));
                    actor.setDist(myDistx);
                    actorsList.add(actor);
                }
            }  catch (final JSONException e) {
              Okoli.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(Okoli.this.getApplicationContext(),
                                "Data error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                }); }
            return true;
        } else {
             Okoli.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(Okoli.this.getApplicationContext(),
                            "Network error",
                            Toast.LENGTH_LONG).show();
                }
            });
            return false;
        }
    }
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        dialog.dismiss();
        adapter.notifyDataSetChanged();
    }

}

还有我的适配器:

public class ActorAdapter extends ArrayAdapter<Actors> {
private Context context;
private ArrayList<Actors> actorList;
private LayoutInflater vi;
private int Resource;
ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);
    this.context = context;
    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    actorList = objects; }
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull final ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvNazov = v.findViewById(R.id.tvNazov);
        holder.tvPlace = v.findViewById(R.id.tvPlace);
        holder.tvPerex = v.findViewById(R.id.tvPerex);
        holder.tvDist = v.findViewById(R.id.tvDist);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
holder.tvNazov.setText(actorList.get(position).getNazov());
    holder.tvPlace.setText(actorList.get(position).getPlace());
    holder.tvPerex.setText(actorList.get(position).getPerex());
    String newDist = String.format("%.1f", actorList.get(position).getDist());
    holder.tvDist.setText(newDist);
    return v;
}
static class ViewHolder {
    TextView tvNazov;
    TextView tvPerex;
    TextView tvPlace;
    TextView tvDist;
    }
}

适配器将actorsList称为数据源,但此变量永远不会清空。

最新更新