Skip to content Skip to sidebar Skip to footer

Fetch Data From Mysql To A List View In Android Only Show The First Item From The Table How To Fix?

i have android application that use connection between android and mysql database using php. after fetching the mysql database i need to display the result in android listview. but

Solution 1:

your problem is bellow code:

for (int i = 0; i < users.length(); i++) {
          JSONObject c = users.getJSONObject(i);

          // Storing each json item in variableString id = c.getString(TAG_ID);
          String user = c.getString(TAG_USER);

          // creating new HashMap
          HashMap<String, String> map = new HashMap<String, String>();

          // adding each child node to HashMap key => value
          map.put(TAG_ID, id);
          map.put(TAG_USER, user);

          // adding HashList to ArrayList
          usersList.add(map);

          return json.getString(TAG_MESSAGE);
    }

you return json.getString(TAG_MESSAGE); in for statement so for just loop for first position so you get fist line of your json, put out return from for statement

your code must be:

for (int i = 0; i < users.length(); i++) {
          JSONObject c = users.getJSONObject(i);

          // Storing each json item in variableString id = c.getString(TAG_ID);
          String user = c.getString(TAG_USER);

          // creating new HashMap
          HashMap<String, String> map = new HashMap<String, String>();

          // adding each child node to HashMap key => value
          map.put(TAG_ID, id);
          map.put(TAG_USER, user);

          // adding HashList to ArrayList
          usersList.add(map);

    }
    return json.getString(TAG_MESSAGE);   <--- move this line after for statement 

Solution 2:

Try it like this, Change yout onPostExecute mehod code like below.

ArrayAdapter<String> adapter = newArrayAdapter<String>(this, R.layout.list_item,
                usersList);
mListView.setCacheColorHint(Color.TRANSPARENT);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
adapter.setNotifyOnChange(false);
mListView.invalidateViews();

where mListview is your ListView Object just replace it's name with your Listview name.

Post a Comment for "Fetch Data From Mysql To A List View In Android Only Show The First Item From The Table How To Fix?"