I'm quite new to android (I'm a newb, I know :/) and am wondering how to proceed.
I am writing an app that takes data from a server, saves it to a SQLite table and displays it in a list. The thing is, I don't know how I should update the listview when data is refreshed from the server. Right now I am just resetting the adapter each time, but surely there is a more efficient method. I've read lots on notifydatasetchanged()
but I'm still not sure how to use it. Any ideas?
ListAdapter:
private class MyListAdapter extends ArrayAdapter<Plants> {
private final LayoutInflater mInflater;
ItemInfo info = new ItemInfo();
public MyListAdapter(Context c) {
super(c, R.layout.list_item, myPlants);
mInflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView;
if (convertView == null) {
itemView = mInflater.inflate(R.layout.list_item, parent, false);
} else {
itemView = convertView;
}
// Find the Plant to work with.
Plant currentPlant = myPlants.get(position);
// Plant number:
TextView plantText = (TextView) itemView
.findViewById(R.id.plantID);
plantText .setText("Plant: " + currentPlant .getPlantID());
// Information string:
TextView infoText = (TextView) itemView.findViewById(R.id.state);
infoText.setText("Status: "
+ info.getStatus(currentPlant.getState()) + " at "
+ info.getLocation(currentPlant.getLocation()));
// Grey out checked out plants
if (currentPlant.getState().equals("3")) {
itemView.setBackgroundColor(Color.LTGRAY);
}
return itemView;
}
}
This method is what I am calling to update the list right now:
private List<Plant> myPlants = new ArrayList<Plant>();
public void updateList() {
// Get everything back from database
try {
SQLHelper getHelper = new SQLHelper(getActivity());
getHelper.open();
myPlants = getHelper.getData();
getHelper.close();
} catch (Exception e) {
iError = 2;
}
// Updating database data into ListVIew
if (myPlants != null) {
ArrayAdapter<Package> adapter = new MyListAdapter(getActivity());
setListAdapter(adapter);
}
}
I'm all out of ideas right now... Thanks!