我使用自定义适配器将一些数据从JSON拉到我的ListView:
adapter = new ActorAdapter(this, R.layout.data_item, actorsList);
JSON包含5个项目。一切都很好,所有项目都显示,但是...因为我也使用位置服务,我需要更新ListView(距离等)。
这发生在这里:
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个项目。这很奇怪,无法找到问题为什么......
附加我的GetContacts异步任务:
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;
}
}
答案 0 :(得分:0)
您的适配器将actorsList
称为数据源,但此变量永远不会被清空。