简体   繁体   English

什么是刷新Android ListView的最佳方法?

[英]What is the best way to refresh a android's listview?

I want to refresh the listView after an insert or delete it in the database. 我想在插入数据库后刷新listView或在数据库中删除它。 I've searched and I found notifyDataSetChanged() , but I don't know how to use it. 我已经搜索过,并且发现notifyDataSetChanged() ,但是我不知道如何使用它。

I used AsyncTask to get the data from the server and insert it into the SQLite database. 我使用AsyncTask从服务器获取数据并将其插入SQLite数据库。 The problem is that the AsyncTask class is in a another file. 问题是AsyncTask类在另一个文件中。 So how can I access the adapter or the list view from the AsyncTask class? 那么如何从AsyncTask类访问适配器或列表视图?

Can someone explain how to do that? 有人可以解释该怎么做吗? Even by means of a different way. 即使通过不同的方式。

Here is my code : 这是我的代码:

public class devicesController extends SherlockFragment {

    devicesDataSource deviceDS;
    static devicesAdapter adapter;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
        View view = inflater.inflate(R.layout.devices, container, false);

        /// Set device's datasource
        deviceDS = new devicesDataSource(getActivity());

        /// Assign the listview from the xml layout
        ListView lv1 = (ListView) view.findViewById(R.id.devicesList);

        /// Getting all devices from the SQLite database
        deviceDS.open();
        List<Devices> allDevices = null;
        try {
            allDevices = deviceDS.getAll();
        } catch (ParseException e) {
            e.printStackTrace();
            Log.i("QUTAYBA", "ERROR showing data");
        }


        /// Set the results rows in a list adapter
        adapter = new devicesAdapter(getActivity(), R.layout.devices_list, allDevices);
        lv1.setAdapter(adapter);

    return view;

    }

The AsyncTask class is: AsyncTask类是:

public class bootstrapTask extends AsyncTask<Void, Void, Void> {
    private static Context thisContext = null;
    static devicesDataSource deviceDS;
    private static devicesAdapter da;


    public bootstrapTask(Context context, devicesAdapter deviceAdapt) {
        thisContext = context;
        deviceDS = new devicesDataSource(thisContext);
        da = deviceAdapt;
    }

    @Override
    protected Void doInBackground(Void... params) {

        String data = dataHelper.checkDataFromServer(thisContext);
        try {
            JSONObject jsonData = new JSONObject(data);
            deviceDS.createFromJsonArray(jsonData.getJSONArray("devices"));

        } catch (JSONException e) {
            Log.i("QUTAYBA", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //da.notifyDataSetChanged();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

}

将适配器作为参数,放入asyncTask类,并在postExcute()方法(该方法由ui线程调用)中刷新ListView。

There is one more good way to achieve what you want without passing adapter object in async task. 在异步任务中不传递适配器对象的情况下,还有一种更好的方法可以实现所需的目标。

From your async task in your onPostExecute() method you can send a broadcast intent and in you activity you can listen for that and call adapater.notifyDataSetChanged(); onPostExecute()方法中的异步任务中,您可以发送broadcast意图,在活动中,您可以侦听并调用adapater.notifyDataSetChanged(); in onRecieve() . onRecieve()

I think the best thing to use in your case is a CursorLoader . 我认为在您的情况下最好使用CursorLoader Among all other tutorial you can find out there, you can check this tutorial . 在所有其他教程中,您可以找到该教程 ,也可以查看本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM