i know this was asked several times but i can not figure out how to load more data to my listview.
I'm using this code that calls the first data for listview, thats ok, after loading data i'm showing a button to load more results but is not showing nothing new to the listview:
EDIT: Now it's loading the data from the server, but it's not appending it to the listview, it's replacing the content with the new one, how can i override that?
EDIT 2: I've added the code of the ListViewAdapter. Thanks to @aegean
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Cargando reportes en tiempo real");
// Set progressdialog message
mProgressDialog.setMessage("Por favor espere");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.videotrafico.com/api/timeline.php?page=1");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("datos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
jsonobject1 = jsonobject.getJSONObject("reporte");
// Retrive JSON Objects
map.put("imagen", jsonobject.getString("imagen"));
map.put("quien", jsonobject.getString("quien"));
map.put("fecha", jsonobject.getString("fecha"));
map.put("reporte", jsonobject.getString("reporte"));
map.put("contenidopost", jsonobject1.getString("contenidopost"));
map.put("imgs", jsonobject1.getString("imgs"));
map.put("video", jsonobject1.getString("video"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
Button btnLoadMore = new Button(MainActivity.this);
btnLoadMore.setText("Cargar m‡s reportes");
// Adding Load More button to lisview at bottom
listview.addFooterView(btnLoadMore);
// Set the adapter to the ListView
listview.setAdapter(adapter);
//LoadMore button
/**
* Listening to Load More button click event
* */
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Starting a new async task
new loadMoreListView().execute();
}
});
// Close the progressdialog
mProgressDialog.dismiss();
}
}
This is the class for loadmore button:
public class loadMoreListView extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
mProgressDialog = new ProgressDialog(
MainActivity.this);
mProgressDialog.setMessage("Cargando más reportes");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
protected Void doInBackground(Void... unused) {
// increment current page
current_page += 1;
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.videotrafico.com/api/timeline.php?page="+current_page);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("datos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
jsonobject1 = jsonobject.getJSONObject("reporte");
// Retrive JSON Objects
map.put("imagen", jsonobject.getString("imagen"));
map.put("quien", jsonobject.getString("quien"));
map.put("fecha", jsonobject.getString("fecha"));
map.put("reporte", jsonobject.getString("reporte"));
map.put("contenidopost", jsonobject1.getString("contenidopost"));
map.put("imgs", jsonobject1.getString("imgs"));
map.put("video", jsonobject1.getString("video"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return (null);
}
protected void onPostExecute(Void unused) {
listview = (ListView) findViewById(R.id.listview);
// closing progress dialog
// get listview current position - used to maintain scroll position
int currentPosition = listview.getFirstVisiblePosition();
// Appending new data to menuItems ArrayList
/*adapter = new ListViewAdapter(
MainActivity.this,
arraylist);
listview.setAdapter(adapter);
*/
ListViewAdapter myExistingAdapter = null;
if(listview.getAdapter() instanceof WrapperListAdapter) {
myExistingAdapter = (ListViewAdapter)((WrapperListAdapter)listview.getAdapter()).getWrappedAdapter();
} else if (listview.getAdapter() instanceof ListViewAdapter) {
myExistingAdapter = (ListViewAdapter)listview.getAdapter();
}
myExistingAdapter.setItems();
myExistingAdapter.notifyDataSetChanged();
// Setting new scroll position
listview.setSelectionFromTop(currentPosition + 1, 0);
mProgressDialog.dismiss();
}
}
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
String coment;
public String img;
public int imga;
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView quien;
ImageView imagen;
TextView reporte;
TextView fecha;
VideoView video;
ImageView imgs;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
// Locate the ImageView in listview_item.xml
imagen = (ImageView) itemView.findViewById(R.id.imagen);
fecha = (TextView) itemView.findViewById(R.id.fecha);
quien = (TextView) itemView.findViewById(R.id.quien);
reporte = (TextView) itemView.findViewById(R.id.reporte);
video = (VideoView) itemView.findViewById(R.id.videos);
imgs = (ImageView) itemView.findViewById(R.id.imgs);
// Capture position and set results to the TextViews
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.IMAGEN), imagen);
fecha.setText(resultp.get(MainActivity.FECHA));
reporte.setText(resultp.get(MainActivity.CONTENIDOPOST));
quien.setText(resultp.get(MainActivity.QUIEN));
if (resultp.get(MainActivity.VIDEO).length() != 0){
imageLoader.DisplayImage(resultp.get(MainActivity.IMGS), imgs);
final String videoplayer = resultp.get(MainActivity.VIDEO);
if (resultp.get(MainActivity.CONTENIDOPOST).length() == 0){
reporte.setVisibility(View.GONE);
}
imgs.setVisibility(View.VISIBLE);
imgs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoplayer));
context.startActivity(intent);
}
});
}
if (resultp.get(MainActivity.IMGS).length() != 0 && resultp.get(MainActivity.VIDEO).length() <= 5){
imageLoader.DisplayImage(resultp.get(MainActivity.IMGS), imgs);
final String imagenview = resultp.get(MainActivity.IMGS);
imgs.setVisibility(View.VISIBLE);
if (resultp.get(MainActivity.CONTENIDOPOST).length() == 0){
reporte.setVisibility(View.GONE);
}
imgs.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
/*
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.parse(imagenview), "image/*");
context.startActivity(intent);
*/
Intent intentBrowseFiles = new Intent(Intent.ACTION_VIEW);
intentBrowseFiles.setDataAndType(Uri.parse(imagenview), "image/*");
//intentBrowseFiles.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentBrowseFiles);
}
});
}
return itemView;
}
}
What i'm doing wrong? Thanks in advance!
Now it's loading the data from the server, but it's not appending it to the listview, it's replacing the content with the new one, how can i override that?
Because you are creating a new adapter:
adapter = new ListViewAdapter(MainActivity.this, arraylist);
try to add new items to your existing adapter.
listview = (ListView) findViewById(R.id.listview);
ListViewAdapter myExistingAdapter = null
if(listview.getAdapter() instanceof WrapperListAdapter) {
myExistingAdapter = (ListViewAdapter)((WrapperListAdapter)listview.getAdapter()).getWrappedAdapter();
} else if (listview.getAdapter() instanceof ListViewAdapter) {
myExistingAdapter = (ListViewAdapter)listview.getAdapter();
}
myExistingAdapter.addItems(items);
myExistingAdapter.notifyDataSetChanged();
Note: To use something like setItems() method, I assume you have a method to set items of your array adapter.
Under your ListViewAdapter class write a method to update your items inside your adapter:
public void addItems(ArrayList itemToAdd) {
if(data != null) {
data.addAll(itemToAdd);
} else {
data = itemToAdd;
}
}
BTW I recommend you to read more abour listView and adapters. Because your implementation is not so good. you have to re-use your views. See an example here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.