Vanessa Larriane Moras Vanessa Larriane Moras - 1 month ago 12
Android Question

Everytime I fetch pdf file, my ArrayAdapter stacks

I need help about how to refresh my ArrayAdapter whenever I click the button to fetch files. It keeps on stacking the new list without clearing the last. Please help, also don't mind my English.

This is my Array adapter

public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PdfHolder holder = null;
if(row == null)
{
LayoutInflater inflater=LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId,parent,false);
holder = new PdfHolder();
holder.textViewName = (TextView) row.findViewById(R.id.textViewName);
holder.textViewUrl = (TextView) row.findViewById(R.id.textViewUrl);
row.setTag(holder);
}
else
{
holder= (PdfHolder) row.getTag();
}

pdf = data.get(position);
holder.textViewName.setText(pdf.getName());
holder.textViewUrl.setText(pdf.getUrl());
return row;
}


class PdfHolder
{
TextView textViewName,textViewUrl;
}


This is my code where I fetch file from my live server

private void getPdfs() {
progressDialog.setMessage("Fetching pdfs from server...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, PDF_FETCH_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject obj = new JSONObject(response);
Toast.makeText (Files.this,obj.getString("message"), Toast.LENGTH_SHORT).show();

JSONArray jsonArray = obj.getJSONArray("pdfs");
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
Pdf pdf = new Pdf();
String pdfName = jsonObject.getString("name");
String pdfUrl = jsonObject.getString("url");
pdf.setName(pdfName);
pdf.setUrl(pdfUrl);
pdfList.add(pdf);
}

pdfAdapter = new PdfAdapter(Files.this, R.layout.list_layout, pdfList);
listView.setAdapter(pdfAdapter);
pdfAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}
);

RequestQueue request = Volley.newRequestQueue(this);
request.add(stringRequest);
}


Can you suggest what should I do? or a code snippet to add or change?

Answer Source

Before

for(int i=0;i<jsonArray.length();i++){

do

pdfList.clear()