Hello, I have the code below that seems to be working correctly, the problem is that the first item in the list either goes blank or copies the data from the last item in the list.
public class TabList extends ListFragment {
JSONObject jsonObj;
JSONArray jsonArray;
TabListAdapter adapter;
Timer myTimer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
@Override
public void onResume () {
super.onResume();
if (myTimer != null) myTimer.cancel();
createTimer();
}
@Override
public void onDestroy () {
super.onDestroy();
myTimer.cancel();
}
@Override
public void onPause () {
super.onDestroy();
myTimer.cancel();
}
private void createTimer () {
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
updateList();
}
}, 0, 10000);
}
public void updateList () {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
SessionManager sessionManager = new SessionManager(getActivity().getApplicationContext());
HashMap<String, String> nearby = sessionManager.getUserDetails();
if (nearby.get("nearby") != null) {
try {
List<User> userList = new ArrayList<User>();
jsonObj = new JSONObject(nearby.get("nearby"));
jsonArray = jsonObj.getJSONArray("rows");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObj = new JSONObject(jsonArray.get(i).toString());
User userNearby;
userNearby = new User();
userNearby.setFirstname(jsonObj.getString("firstname"));
userNearby.setLastname(jsonObj.getString("lastname"));
userNearby.setPlate(jsonObj.getString("plate"));
userNearby.setEmail(jsonObj.getString("email"));
userNearby.setLat(jsonObj.getDouble("lat"));
userNearby.setLng(jsonObj.getDouble("lng"));
userNearby.setStatus(jsonObj.getString("status"));
userNearby.setDistance(jsonObj.getDouble("distance"));
userList.add(userNearby);
}
adapter = new TabListAdapter(getActivity(), userList);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}
}
I do not know if I'm forgetting something because I'm applying dapter.notifyDataSetChanged (); and also do not see need to use a userlist.clear (), correct?
The TabListAdapter code:
package br.com.nerd.adapter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import br.com.nerd.autodate.R;
import br.com.nerd.autodate.model.User;
public class TabListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<User> userItems;
public TabListAdapter (Activity activity, List<User> userItems) {
this.activity = activity;
this.userItems = userItems;
}
@Override
public int getCount() {
return userItems.size();
}
@Override
public Object getItem(int location) {
return userItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) convertView = inflater.inflate(R.layout.list_row, null);
ImageView picture = (ImageView) convertView.findViewById(R.id.picture);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView plate = (TextView) convertView.findViewById(R.id.plate);
TextView distance = (TextView) convertView.findViewById(R.id.distance);
if (position > 0) {
User m = userItems.get(position);
plate.setText(m.getPlate() + " (" + m.getStatus() + ")");
name.setText(m.getFirstname() + " " + m.getLastname());
distance.setText(m.getDistance().toString());
}
return convertView;
}
}