How to find out when the list is finished by downloading data on Android

I currently have a list displaying information about what needs to be done.

When the user clicks an item in the list, the application checks which item was clicked and set as completed.

If the item was set as completed, it will display an image with verification; This image is part of a list item.

everything works well. If I scroll, exit the action and open it again, uncheck the box, everything will work fine and will display or hide the image of the corresponding list item.

BUT my problem is this:

I have 2 buttons for sorting list items, in alphabetical order and date order. When I click on them, they work great. BUT, the application does not display the image of elements that have been checked.

I know this because in order to do the checks, I need the listview to fully load. I mean, to show all the information in my activity.

My question is:

How do I know when a list view is populated or loaded so that I can call a method that checks which items have been checked to show the image?

I have isfocused (), onfocuschanged (), onWindowChangeState () and all these types of methods, but none of them work to run my method.

Is there a way to find out when the list is populated so that the check is displayed on the displayed items without any user interaction?

Thanks for your time and help.

PD: I already have a part in which the user views the list, this part will take care.

I use SimpleCursorAdapter to populate the list.

Here I fill in the list:

public void mostrarLista(){ Cursor c = admin.obtenerCursorGastosFijos(ordenadoPor); // The desired columns to be bound String[] columnas = new String[] {"_id", "Descripcion", "Costo_Estimado", "Fecha_Pago"}; // the XML defined views which the data will be bound to int[] views = new int[] {R.id.IdGstFijo, R.id.DescGstFijo, R.id.CostoGstFijo, R.id.FechaGstFijo }; // create the adapter using the cursor pointing to the desired data //as well as the layout information dataAdapter = new SimpleCursorAdapter(this, R.layout.lista_gastos_fijos, c, columnas, views, 0); listaGastos = (ListView) findViewById(R.id.listaGastosFijos1); // Assign adapter to ListView listaGastos.setAdapter(dataAdapter); listaGastos.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> Listview, View v, int position, long id) { // TODO Auto-generated method stub Cursor cursor = (Cursor) Listview.getItemAtPosition(position); String idGst=cursor.getString(cursor.getColumnIndexOrThrow("_id")); dialogoOpcionesGst(Integer.parseInt(idGst), v).show(); return true; } }); listaGastos.setOnScrollListener(new OnScrollListener(){ @Override public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onScrollStateChanged(AbsListView arg0, int arg1) { // TODO Auto-generated method stub mostrarItemsPagados(listaGastos); } }); } 

This is the method I did to navigate the elements that are visible and see if they were marked or not.

 public void mostrarItemsPagados(ListView lista){ for (int i = 0; i <= lista.getLastVisiblePosition() - lista.getFirstVisiblePosition(); i++){ View item = (View)lista.getChildAt(i); ImageView img = (ImageView)item.findViewById(R.id.imgPagado); if(admin.existeGstFijoReal(Integer.parseInt(((TextView)item.findViewById(R.id.IdGstFijo)).getText().toString()))){ img.setImageResource(R.drawable.img_check); } else img.setImageDrawable(null); } } 

this is the method i use to sort the list:

  public void ordenarNombre(View v){ ordenadoPor=1; mostrarLista(); } 

and well, the layout of the item inside the list:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/IdGstFijo" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="2" android:visibility="gone" android:gravity="center" android:lines="4" /> <TextView android:id="@+id/DescGstFijo" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="2" android:gravity="center" android:lines="4" /> <TextView android:id="@+id/CostoGstFijo" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="2" android:gravity="center" android:lines="4"/> <TextView android:id="@+id/FechaGstFijo" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="2" android:gravity="center" android:lines="4" /> <ImageView android:id="@+id/imgPagado" android:layout_height="match_parent" android:layout_width="0dp" android:layout_weight="1" /> 

+10
android listview
source share
6 answers

Ok, I manage to solve the problem.

All I had to do was override the getView method, as said.

But, since I used the extended SimpleCursorAdapter class, I had to do something like this:

 public View getView(int position, View convertView, ViewGroup parent){ View item = super.getView(position, convertView, parent); //Validation code for drawing or not the check image return item; } 

So this did the job, and now every time the list is viewed reordered or drawn or scrolled, the views are updated correctly.

Thanks guys for the help

+2
source share
  • Setting addOnLayoutChangeListener in the list
  • Call .notifyDataSetChanged ();
  • This will disable OnLayoutChangeListener after completion
  • Remove Listener

     listaGastos.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { listaGastos.removeOnLayoutChangeListener(this); Log.d(TAG, "list got updated, do what ever u want"); } }); dataAdapter.notifyDataSetChanged(); 
+9
source share

My decision:

 list = (ListView) findViewById(R.id.list); list.getViewTreeObserver().addOnPreDrawListener(mOnPreDrawListener); ... private final ViewTreeObserver.OnPreDrawListener mOnPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { mMessageList.getViewTreeObserver().removeOnPreDrawListener(this); // Do what you want to do on data loading here return true; } }; 
+4
source share

It worked for me ( mc.android.developer ):

 listview.getViewTreeObserver().addOnPreDrawListener(OnPreDrawListener); private final ViewTreeObserver.OnPreDrawListener OnPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { //do changes return true; } }; 
+4
source share

You should not rely on the โ€œfinish loading dataโ€ condition, instead you should set the visibility of the image on the fly.

You should do something similar in your getView method of your list view adapter.

 @Override public View getView(int position, View convertView, ViewGroup parent) { final ItemView item; if (convertView == null) { item = mLayoutInflator.inflate(R.layout.listlayout) } else { item = (ItemView) convertView; } ImageView image = (ImageView)item.findViewById(R.id.yourimageidinitemview); if(mTodoTask[posistion].isChecked) { image.setVisibility(View.Visible); } else { image.setVisibility(View.Invisible); } return item; } 
+3
source share

You can use a handler to perform this task, for example:

In your activity, add a handler like any other property.

 private Handler mListViewDidLoadHanlder = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message message) { //Do whatever you need here the listview is loaded return false; } }); 

And inside the getView method of your list, you do a comparison to see if the current position is the last, and thus it will end (just put it before returning):

 public View getView(int position, View convertView, ViewGroup parent) { //Your views logic here if (position == mObjects.size() - 1) { mViewDidLoadHanlder.sendEmptyMessage(0); } return convertView; } 
+2
source share

All Articles