从去到Listactivity Listfragment [英] Going from Listactivity to Listfragment

查看:116
本文介绍了从去到Listactivity Listfragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天我一直在试图去从目前的listactivity到listfragment的。我将数据加载到我Listactivity通过它的伟大工程的ArrayAdapter。数据首先从远程服务器然后插入到SQLite数据库收集。当数据为presented我将数据加载到一个定制的BaseAdapter。所有这一切的伟大工程。我真的想用listfragments更新我的UI。这尤其是因为我有谁使用平板电脑的许多客户。

For the last couple of days i have been trying to go from current listactivity to listfragment. I load the data into my Listactivity through a an ArrayAdapter which works great. The data is first collected from a remote server then inserted into an SQLite database. When the data is presented i load the data into an custom BaseAdapter. All of this works great. I would really like to update my UI by using listfragments. This especially because I have many clients who use a tablet.

我知道如何针对平板电脑创建更大的意见,但希望我的应用程序是在一个iPad psented像$ P $。我的理解是可能的片段做到这一点。到目前为止,我已经设法与静态数据工作listfragments。问题是我不知道如何加载在阵列适配器插入listfragment present的数据。

I know how to create larger views for tablets, but would like my apps to be presented like in an iPad. To my understand it is possible to do this with fragments. So far i have managed to get the listfragments working with static data. The problem is I don't know how to load the data that is present in the array adapter into the listfragment.

我用异步任务来获取,存储和present在ListView中的数据。

I use Async tasks to retrieve , store and present the data in a listview.

谁能帮我加载数据从一个异步的任务变成了listfragment?目前我一切都加载到在listactivity列表​​视图。

Could anyone help me with loading data from an async task into a listfragment? Currently I load everything into a listview in a listactivity.

感谢

推荐答案

从本质上讲它必须是相同的。只是该容器是不同的(活动到片段),内部实现最好应保持不变。所以,我要说创建的AsyncTask使用相同的组件,里面onPostExecute()方法中,你可以做一个setListAdapter(适配器),类似如下:

Essentially it has to be the same. Just the container is different(Activity to Fragment), the internal implementation should ideally remain the same. So i would say create AsyncTask use the same components, inside onPostExecute() method, you can do a setListAdapter(adapter), something like below:

class MyFragment extentsListFragment{

//define constructors if any 
public void onResume(){

    LoadData loadData = new LoadData();
    loadData.execute();

}
//unimplementedMethods





private class LoadData extends AsyncTask<Void, Void, Void> {

    private Object object = null;

    protected Void doInBackground(Void... params) {

        //make a call to the server and save the data in SQLite DB
        // Make a call to the database to get your data
        object = getObjectFromDB();

        return null;
    }

    protected void onPostExecute(Void result) {
        if(object == null || object.length <= 0) {
            setListAdapter(null);
            return;
        }




        // assuming object has your data, say it's an Array  List, work on it and setListAdapter
        adapter = ...
        setListAdapter(adapter)
    }

    private Object getObjectFromDB() {
        // do whatever
        return object;
    }
}

}

这篇关于从去到Listactivity Listfragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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