As the title says, I have a mySQL database that has data on it. I want my android app to retrieve that data and insert it into a ListView. I don’t think I’m doing it correctly because my current code; although not producing any errors, doesn’t work. Here is my code:
I’d just like to point out that when I insert that data into a TextView it shows up on my app just fine. So it must be that I’m doing something wrong with my ListView. I just don’t know what.
Just to make it easier to read these are the parts of my ListView:
At the top of my code
List<String> nameData = new ArrayList<String>();
The for loop for my JSON Parsing:
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
currentList = (ListView) findViewById(R.id.list);
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
nameData.add(drivername = json_data.getString("Driver_full_name"));
nameData.add(drivesfor = json_data.getString("Drives_for"));
}
I do have a catch here as you can see in the pastebin link, just haven’t posted it.
and the onPostExecute
protected void onPostExecute(String output){
currentList.setAdapter(new ArrayAdapter<String> (CurrentSeasonDrivers.this, android.R.layout.simple_list_item_2, nameData));
Toast.makeText(CurrentSeasonDrivers, "DONE!", Toast.LENGTH_LONG).show();
}
Just to reiterate; a TextView works fine when pulling the data so the rest of the code is correct.
Really appreciate any help given. Thanks.
I am not sure what the exact problem is in your code but I have a feeling it has to do with your adapter. I have suggestion on a way to do it that should work and that is better and more flexible which is basically creating your own adapter.
First make a small Driver class which will be used as the source of data
}
Now create the adapter which will be used for creating the views
Now you need the view which will be used to display each listview row
And now in your code you simply create the arraylist of drivers, create an adapter out of that, and set that adapter on your listview.
You should be able to just copy paste the all of these things into their own classes and it should work.. and replace your listview code with the one I gave at the bottom..
This should work, let me know if you have any questions