RED_
RED_

Reputation: 3007

Getting a ListView to work when grabbing data from a mySQL server

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:

http://pastebin.com/sZy5dAzS

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.

Upvotes: 1

Views: 596

Answers (1)

krilovich
krilovich

Reputation: 3505

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

public class DriverItem {

    private String name;
    private String driversFor;

    public DriverItem(String n, String d) {
        super();
        this.name = n;
        this.driversFor = d;
    }

    // Getter and setter methods for all the fields.
    public String getName() {
        return name;
    }

    public String getDriversFor() {
        return driversFor;
    }

}

Now create the adapter which will be used for creating the views

public class DriverListItemAdapter extends BaseAdapter {
    private List<DriverItem> listDriverItem;
    private Context context;

    public DriverListItemAdapter(Context ctx, List<DriverItem> list)
    {
        context = ctx;
        listDriverItem = list;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        listDriverItem.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listDriverItem.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        // TODO Auto-generated method stub
        DriverItem driver = listDriverItem.get(position);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.list, null);
        TextView name = (TextView)convertView.findViewById(R.id.txtName);
        name.setText(driver.getName());

        return convertView
    }
}

Now you need the view which will be used to display each listview row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/txtName"
        android:layout_height="wrap_parent"
        android:layout_width="fill_parent" 
        />
</LinearLayout>

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.

DriverListItemAdapter adapter;
ListView currentList = (ListView) findViewById(R.id.list);
ArrayList<DriverItem> listOfDriverListItem;

for(int i = 0; i < jArray.length(); i++){
            jObj = jArray.getJSONObject(i);        
            listOfDriverListItem.add(newDriverItem(jObj.getString("Driver_full_name"),jObj.getString("Drives_for")));
        }

adapter = new DriverListItemAdapter(this, listOfDriverListItem);
currentList.setAdapter(adapter);

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

Upvotes: 1

Related Questions