I have been for the past several hours trying to do something that I think should be trivial. I keep reading answers on stackoverflow but its not jiving with me. people show examples of ArrayAdapters
accepting string ArrayLists
on stackoverflow. This is just not working for me.
Populating a ListView using an ArrayList?
I am trying to understand what is going on here. If I am dealing with an arraylist of type String that means that the only thing I can bind the data to is a textview and certainly not a listview unless I create my own custom link adapter class? This just seems like I am missing some information. I would thing this problem would be so common a solution would have been made. My code is below and I am getting the "E/ArrayAdapter(5106): You must supply a resource ID for a TextView
" error.
public class Favorites extends Activity{
UserFunctions userFunctions = new UserFunctions();
ArrayAdapter<String> arrayAdapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favoritespage);
arrayAdapter1 = new ArrayAdapter<String>(Favorites.this,android.R.layout.activity_list_item);
new DownloadDataTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main_screen, menu);
return true;
}
private class DownloadDataTask extends AsyncTask<JSONArray, JSONArray, ArrayList<String> > {
@Override
protected ArrayList<String> doInBackground(JSONArray... params) {
JSONArray json = userFunctions.ziplistrequest("39", "-74", "50");
ArrayList<String> zipcodes = new ArrayList<String>();
for(int i=0; i < json.length() ; i++) {
JSONObject jarray = null;
try {
jarray = json.getJSONObject(i);
String zip = jarray.getString("ZIPCODE");
zipcodes.add(zip);
arrayAdapter1.add(zip);
Log.d(zip,"Output");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return zipcodes;
}
protected void onPostExecute(ArrayList<String> result){
ListView listView = (ListView) findViewById(R.id.list);
arrayAdapter1.addAll(result);
listView.setAdapter(arrayAdapter1);
}
}
}