如何知道listview何时完成在Android上的数据加载 [英] how to know when listview is finished loading data on android

查看:83
本文介绍了如何知道listview何时完成在Android上的数据加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个列表视图,显示必须完成的事情的信息.

I currently have a list view that displays information of things that have to be done.

当用户单击列表视图上的项目时,应用程序将检查已单击的项目并设置为完成.

When an user clicks the item on the listview, then the app checks which item has been clicked and set as done.

如果已将项目设置为完成,它将显示带有勾选的图像视图;此imageview是listview项的一部分.

If the item has been set as done, it will show an imageview with a check; this imageview is part of the item of the listview.

一切正常.如果我滚动,退出活动并再次将其打开,取消选中该项目,则一切正常,并显示或隐藏相应列表视图项目的imageview.

everything is working well. If I scroll, exit the activity and open it again, uncheck the item, everything works well and shows or hide the imageview of the corresponding listview item.

但是我的问题是这个:

我有2个按钮可以按字母顺序和日期顺序对列表视图项进行排序.当我单击它们时,它们效果很好.但是,该应用程序不会显示已检查项目的图像视图.

I have 2 buttons to sort the list view items, alphabetically and in order of date. When I click them, they work great. BUT, the app doesn't show the imageview of the items that have been checked.

我知道这是因为要使检查出现,我需要将Listview完全加载.我的意思是,在活动中显示所有信息.

I know this is because in order to make the checks appear, I need the listview to be fully loaded. I mean, show in the activity with all the info.

我的问题是:

我怎么知道列表视图何时完成填充或加载,以便我可以调用确保已检查哪些项目以显示图像视图的方法?

How can I know when the list view is done populating or loading, so that I can call the method that make sure which items have been check to show the image view?

我使用过isfocused(),onfocuschanged(),onWindowChangeState()以及所有这些类型的方法,但是它们都不触发我的方法.

I have use isfocused(), onfocuschanged(), onWindowChangeState(), and all those type of methods, but none of them works to trigger my method.

有什么方法可以知道何时填充列表视图以使检查显示在所显示的项目上,而无需任何用户交互?

Is there any way to be able to know when the listview gets populated to make the check appear on the items that are been show, without any user interaction?

感谢您的时间和帮助.

PD:我已经有了用户滚动列表的部分,该部分已经处理完毕.

PD: I already have the part where the user scroll the list, that part is taken care of.

我正在使用SimpleCursorAdapter填充列表视图.

I'm using a SimpleCursorAdapter to fill the listview.

这是我填写列表视图的地方:

This is where I fill the list view:

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 that I did to navigate the items that are visible and see if they were checked 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 is:

<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"

    />

推荐答案

好,我设法解决了这个问题.

Ok, i manage to solve the problem.

我所要做的就是按照上述方法重写getView方法.

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

但是,由于我使用的是SimpleCursorAdapter扩展类,因此我必须执行以下操作:

But, since Im using a SimpleCursorAdapter extended 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, that did the work, and now everytime the listview is reordered or drawn or scrolled the views are refreshed correctly.

感谢大家的帮助

这篇关于如何知道listview何时完成在Android上的数据加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆