Как правило, вы возвращаете ArrayList элементов обратно в свою активность и передаете его в свой адаптер. Вот один пример кода в Деятельности:
private DataBaseHelper myDbHelper;
private ListView mLocationList;
private TextView mEmptyList;
static ArrayList<LocationAddress> locationList = new ArrayList<LocationAddress>();
private LocationsListAdapter adapter;
....
thisContext = this;
myDbHelper = new DataBaseHelper(thisContext);
mLocationList = (ListView) findViewById(R.id.list_browse);
mLocationList.setOnItemClickListener(new LocationClickListener());
// at this point locationList is empty
adapter = new LocationsListAdapter(thisContext, locationList);
mLocationList.setAdapter(adapter);
// now, get the locationList by calling a background Async Task
new GetLocations().execute("");
private class GetLocations extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(thisContext);
dialog.setMessage("Loading, please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String... urls) {
String results = "";
locationList = new ArrayList<LocationAddress>();
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
results = "Error Opening Database";
throw sqle;
}
Cursor c = null;
try{
c = myDbHelper.getLocations();
if (c != null) {
if (c.moveToFirst())
{
do {
LocationAddress location = new LocationAddress();
location.setID(c.getInt(0));
location.setName(c.getString(1));
location.setAddress(c.getString(2));
location.setLatitude(c.getDouble(3));
location.setLongitude(c.getDouble(4));
location.setType(c.getString(5));
location.setSortOrder(c.getInt(6));
locationList.add(location);
} while (c.moveToNext());
}
}
if (c != null) {
if (!c.isClosed()) {
c.close();
}
}
} catch (Throwable t){
if (c != null) {
if (!c.isClosed()) {
c.close();
}
}
}
try {
myDbHelper.close();
}catch(SQLException sqle){
results = "SQL Error";
throw sqle;
}
return results;
}
protected void onPostExecute(String results) {
dialog.dismiss();
if (results != null) {
if (results.length() > 0) {
alertMessage("Get Locations", results);
} else {
// This refreshes the adapter with a full list of items
adapter = new LocationsListAdapter(thisContext, locationList);
mLocationList.setAdapter(adapter);
}
}
}
} // private Class
А класс Adapter выглядит так:
public class LocationsListAdapter extends BaseAdapter {
private Context context;
private ArrayList<LocationAddress> locationItems;
public LocationsListAdapter(Context context, ArrayList<LocationAddress> locationItems){
this.context = context;
this.locationItems = locationItems;
}
@Override
public int getCount() {
return locationItems.size();
}
@Override
public Object getItem(int position) {
return locationItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.location_list_item, null);
}
LinearLayout topLayout = (LinearLayout) convertView.findViewById(R.id.location_top_layout);
TextView txtSectionTitle = (TextView) convertView.findViewById(R.id.location_section_title);
TextView txtTitle = (TextView) convertView.findViewById(R.id.location_title);
TextView txtAddress = (TextView) convertView.findViewById(R.id.location_address);
String previous_section_title = "";
String section_title = "";
if (position > 0) {
previous_section_title = locationItems.get(position - 1).getType();
}
section_title = locationItems.get(position).getType();
String location_title = locationItems.get(position).getName();
String location_address = locationItems.get(position).getAddress();
txtSectionTitle.setText(section_title);
if (section_title.contentEquals(previous_section_title)) {
topLayout.setVisibility(View.GONE);
} else {
topLayout.setVisibility(View.VISIBLE);
}
txtTitle.setText(location_title);
txtAddress.setText(location_address);
return convertView;
}
}