Reputation: 93
I have a ListView on my main Activity and a button to fetch the JSON.
If I repeat the Button click, the same JSON elements is added to the ListView.
How can I clear the ListView if I click the Button again ?
CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Vehicle> vehicleItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Vehicle> vehicleItems) {
this.activity = activity;
this.vehicleItems = vehicleItems;
}
@Override
public int getCount() {
return vehicleItems.size();
}
@Override
public Object getItem(int location) {
return vehicleItems.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);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Vehicle m = vehicleItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
String genreStr = "";
for (String str : m.getGenre()) {
genreStr += str + ", ";
}
genreStr = genreStr.length() > 0 ? genreStr.substring(0,
genreStr.length() - 2) : genreStr;
genre.setText(genreStr);
// release year
year.setText(String.valueOf(m.getYear()));
return convertView;
}
}
SearchActivity
public class SearchActivity extends Activity implements View.OnClickListener {
// Log tag
private static final String TAG = SearchActivity.class.getSimpleName();
// Vehicles json url
private static final String url = "https://api.myjson.com/bins/3u7ty";
private SweetAlertDialog pDialog;
private List<Vehicle> vehicleList = new ArrayList<Vehicle>();
private ListView listView;
private CustomListAdapter adapter;
private Button buttonGet2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
buttonGet2 = (Button) findViewById(R.id.buttonGet2);
buttonGet2.setOnClickListener(this);
}
private void getData() {
listView=(ListView) findViewById(R.id.list2);
adapter=new CustomListAdapter(this,vehicleList);
listView.setAdapter(adapter);
final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
pDialog.setTitleText("Loading");
pDialog.setCancelable(true);
// Showing progress dialog before making http request
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest vehicleReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
pDialog.dismissWithAnimation();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Vehicle vehicle = new Vehicle();
vehicle.setTitle(obj.getString("title"));
vehicle.setThumbnailUrl(obj.getString("image"));
vehicle.setRating(((Number) obj.get("rating"))
.doubleValue());
vehicle.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
vehicle.setGenre(genre);
// adding vehicle to vehicles array
vehicleList.add(vehicle);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.dismissWithAnimation();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(vehicleReq);
}
@Override
public void onClick(View v) {
getData();
}
}
Upvotes: 0
Views: 467
Reputation: 191738
Along with clearing the data, just make that method only responsible for getting the data, not repeating initializing the adapter and list.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
buttonGet2 = (Button) findViewById(R.id.buttonGet2);
buttonGet2.setOnClickListener(this);
// Moved these lines
listView = (ListView) findViewById(R.id.list2);
adapter = new CustomListAdapter(this,vehicleList);
listView.setAdapter(adapter);
}
private void getData() {
vehicleList.clear(); // Added this
final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
pDialog.setTitleText("Loading");
pDialog.setCancelable(true);
Upvotes: 1
Reputation: 9870
You are adding the content of Json object
to your vehicleList
again and again. The vehicleList
still holds the old values. Then you catch the values from the Json object again and add it again to the vehicleList. To get rid of this, clear the list:
private void getData() {
vehicleList.clear();
listView=(ListView) findViewById(R.id.list2);
adapter=new CustomListAdapter(this,vehicleList);
//rest of your code
}
Upvotes: 1