1

I want to refresh the listView after an insert or delete it in the database. I've searched and I found notifyDataSetChanged(), but I don't know how to use it.

I used AsyncTask to get the data from the server and insert it into the SQLite database. The problem is that the AsyncTask class is in a another file. So how can I access the adapter or the list view from the AsyncTask class?

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:

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);
    }

}
4

3 回答 3

1

还有另一种好方法可以实现您想要的,而无需在异步任务中传递适配器对象。

从您的onPostExecute()方法中的异步任务中,您可以发送一个意图,在您的活动中,broadcast您可以监听并调用adapater.notifyDataSetChanged();.onRecieve()

于 2013-05-05T10:17:54.543 回答
1

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

于 2013-05-04T02:42:26.500 回答
0

我认为在您的情况下使用的最好的东西是CursorLoader。在您可以找到的所有其他教程中,您可以查看本教程

于 2013-05-04T04:03:16.120 回答