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