I want to fetch names from mysql and display in listview in android. I have used JSON parsing for that. Data is fetch successfully but not display in listview. layout of listview shows nothing. this is xml code of list view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.viren.cable.custlist">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview">
</ListView>
xml code of items of listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listname"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
now java code of listactivity
public class custlist extends ActionBarActivity {
String json="";
private static final String TAG_RESULTS="result";
private static final String TAG_NAME="name";
JSONArray people=null;
ArrayList<HashMap<String,String>> personlist;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custlist);
list=(ListView) findViewById(R.id.listview);
personlist=new ArrayList<HashMap<String, String>>();
new getDataJSON().execute();
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(json);
people = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < people.length(); i++) {
JSONObject c = people.getJSONObject(i);
String name = c.getString(TAG_NAME);
HashMap<String, String> persons = new HashMap<String, String>();
persons.put(TAG_NAME, name);
personlist.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
custlist.this, personlist, R.layout.activity_custlist,
new String[]{TAG_NAME},
new int[]{R.id.listname}
);
list.setAdapter(adapter);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
e.printStackTrace();
}
}
class getDataJSON extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new
BasicHttpParams());
HttpPost httppost = new
HttpPost("http://www.vgeek.in/custname.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = "";
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
Log.e("log_tag", "connection success ");
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null)
{
// sb.append(line + "\n");
sb.append(line);
}
inputStream.close();
result = sb.toString();
} catch (Exception e) {
// Oops
Log.e("log_tag", "Error converting result "+e.toString());
}
return result;
}
@Override
protected void onPostExecute(String result){
json=result;
showList();
}
}
}
here is my logcat and in this i have checked that my data is fetchd or not but it fetched.
03-01 17:44:33.186 2829-2829/com.example.viren.cable D/gralloc_goldfish﹕
Emulator without GPU emulation detected.
03-01 17:44:36.174 2829-2829/com.example.viren.cable I/Choreographer﹕
Skipped 85 frames! The application may be doing too much work on its main
thread.
03-01 17:44:40.825 2829-2846/com.example.viren.cable E/log_tag﹕
connection success
03-01 17:44:41.188 2829-2829/com.example.viren.cable I/Choreographer﹕
Skipped 97 frames! The application may be doing too much work on its main
thread.
03-01 17:44:41.191 2829-2829/com.example.viren.cable I/log_tag﹕ name vik
03-01 17:44:41.191 2829-2829/com.example.viren.cable I/log_tag﹕ name nik
03-01 17:44:41.191 2829-2829/com.example.viren.cable I/log_tag﹕ name
ravi