0

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
  • replace `ListAdapter adapter = new SimpleAdapter( custlist.this, personlist, R.layout.activity_custlist, new String[]{TAG_NAME}, new int[]{R.id.listname} );` with `ListAdapter adapter = new SimpleAdapter( custlist.this, personlist, android.R.layout.simple_list_item_1, new String[]{TAG_NAME}, new int[]{R.id.listname} );` – ELITE Mar 01 '16 at 10:19
  • @ELITE still same thing happen.. no data display in listview. – Virendra Singh Mar 01 '16 at 12:16
  • provide your `Logcat` to let us know the error it is throwing or the success result. – ELITE Mar 01 '16 at 12:19
  • @ELITE here is my latest running logcat – Virendra Singh Mar 01 '16 at 12:23
  • how do you know you're getting result very well? Add `Log.d("response", result);` after `result = sb.toString();` this line in `doInBackground` method of `AsyncTask` – ELITE Mar 01 '16 at 12:30
  • what is `R.layout.activity_custlist`, i think it should be the Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to" – ELITE Mar 01 '16 at 13:44
  • @ELITE `R.layout.activity_custlist` is a layout of listview. and my data is fetched successfully bcoz i m getting name from database as u can see in last of `Logcat` – Virendra Singh Mar 01 '16 at 14:34
  • you should provide the name of listview item instead of activity – ELITE Mar 01 '16 at 14:38
  • @ELITE thnx.. your first comment works well.. but instead of `android.R.layout.simple_list_item_1` there is `R.layout.item_list` – Virendra Singh Mar 01 '16 at 18:27
  • great..close the question then. – ELITE Mar 02 '16 at 02:23

3 Answers3

1

You should tell it after you add data, that there was a change and it should refresh. You do this by calling adapter.notifyDataSetChanged();.

If this doesnt help, here is a nice answer that will help you out.

Community
  • 1
  • 1
ZassX
  • 1,369
  • 2
  • 14
  • 35
0

Replace

 ListAdapter adapter = new SimpleAdapter( custlist.this, personlist,   
 R.layout.activity_custlist, new String[]{TAG_NAME}, new int[]  
 {R.id.listname}    
 ); 

with

 ListAdapter adapter = new SimpleAdapter( custlist.this, personlist,    
 R.layout.item_list, new String[]{TAG_NAME}, new int[]
 {R.id.listname} );
0

Replace these lines

ListAdapter adapter = new SimpleAdapter(
            custlist.this, personlist, R.layout.activity_custlist,
            new String[]{TAG_NAME},
            new int[]{R.id.listname}
    );
list.setAdapter(adapter);

by the following

ListAdapter adapter = new SimpleAdapter(
            custlist.this, personlist, R.layout.item_list,
            new String[]{TAG_NAME},
            new int[]{R.id.listname}
    );
list.setAdapter(adapter);

and it'll work fine.

DESCRIPTION

You should know about SimpleAdapter constructor

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

here Context: The context where the View associated with this SimpleAdapter is running

List: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

int: Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

String: A list of column names that will be added to the Map associated with each item.

int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

Community
  • 1
  • 1
ELITE
  • 5,815
  • 3
  • 19
  • 29