Search code examples
androidlistviewsimpleadapter

List view with Simple Adapter not showing text


I have asked similar question today where the problem was with Array adapter. I have tried now to set up List view with Simple adapter because i need header row and subrow with content.

Program starts without errors but screen is blank.

EDIT: Ive updated code, it now runs without problem. I think that problem was in Jsoup query for contentNode.

Here is the code:

    package com.example.studentservis;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import android.R.string;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.content.Context;
    import android.view.Menu;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;

    public class MainActivity extends Activity  {



        List<String> headersList = new ArrayList<String>();
        List<String> contentList = new ArrayList<String>();
        ListView listview;
        StableArrayAdapter adapter1;
        String from[] = {"naslov","sadrzaj"};
        int [] to =  { android.R.id.text1, android.R.id.text2 };

        SimpleAdapter simpleAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
            listview = (ListView) findViewById(R.id.listView1);

            new SampleAsyncTask().execute();


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }




        public class SampleAsyncTask extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    webRequest();


                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

                List<Map<String, String>> data = new ArrayList<Map<String, String>>();

                for(int i = 0; i < headersList.size(); i++)
                {
                    Map<String,String>oglas = new HashMap<String,String>(2);
                    oglas.put("naslov", headersList.get(i));
                    oglas.put("sadrzaj", contentList.get(i));
                    data.add(oglas);
                }
                simpleAdapter = new SimpleAdapter(MainActivity.this, data, android.R.layout.simple_list_item_2, from, to);

                listview.setAdapter(simpleAdapter);
            }

        }



        private class StableArrayAdapter extends ArrayAdapter<String>{

            HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

            public StableArrayAdapter(Context context, int textViewResourceId,
                    List<String> objects) {
                super(context, textViewResourceId, objects);
                // TODO Auto-generated constructor stub
                for (int i = 0; i < objects.size(); ++i) {
                    mIdMap.put(objects.get(i), i);
                }
            }

            @Override
            public long getItemId(int position) {
                String item = getItem(position);
                return mIdMap.get(item);
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }
        }



        public void webRequest() throws Exception{
            String servisURL = "http://www.sczg.unizg.hr/student-servis/";

            Document doc = Jsoup.connect(servisURL).get();

            Elements jobNode = doc.select("div.jobBox");
            Elements headersNode = jobNode.select("h1");
            Elements contentNode = jobNode.select("div.content");


            for(int i = 0; i < headersNode.size(); i++){
                headersList.add(headersNode.get(i).text());
            }

            for(int i = 0; i < contentNode.size(); i++){
                contentList.add(contentNode.get(i).text());
            }



        }

    }

Solution

  • Not sure if this is the best way. I have modified the above. You can use the below for reference. Check the snap shot if the data in the listview is right.( coz i don't understand the text data). If the data in the list is not right modify the below accoridng to your requirements.

    public class test extends Activity  {
    
    String s;
    List<String> headersList = new ArrayList<String>();
    List<String> contentList = new ArrayList<String>();
    ListView listview;
    
    //    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    //    List<Map<String, String>> data2 = new ArrayList<Map<String, String>>();
    List<String> data = new ArrayList<String>();
    
    SimpleAdapter simpleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
    
        listview = (ListView) findViewById(R.id.list);
    
        new SampleAsyncTask().execute();
    
    
    }
    
    public class SampleAsyncTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(Void... params) {
            try {
                webRequest();
                //setUpSimpleAdapter();
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
           // List<Map<String, String>> data = new ArrayList<Map<String, String>>();
            Map<String,String>oglas = new HashMap<String,String>();
            for(int i = 0; i < headersList.size(); i++)
            { 
                data.add(headersList.get(i));    
            }
             //my guess contentList is null. 
             //  can't understand the contents.
             // in that cases you can use headerList directly instead of adding to data
             // used array adapter
            ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (test.this,android.R.layout.simple_list_item_1,data);
            // instead of data you can use headerList.   
            listview.setAdapter(adapter1);
           // listview.setAdapter(simpleAdapter);
        }
    
    }
    
    
    public void webRequest() throws Exception{
        String servisURL = "http://www.sczg.unizg.hr/student-servis/";
        String response = Jsoup.connect(servisURL).response().toString();
        System.out.println(response);
        Document doc = Jsoup.connect(servisURL).get();
    
        Elements jobNode = doc.select("div.jobBox");
        Elements headersNode = jobNode.select("h1");
        Elements contentNode = jobNode.select("content");
    
    
        for(int i = 0; i < headersNode.size(); i++){
            headersList.add(headersNode.get(i).text());
        }
    
        for(int i = 0; i < contentNode.size(); i++){
            contentList.add(contentNode.get(i).text());
        }
    }
    }
    

    Edit:

    The problem is this

          Elements contentNode = jobNode.select("content"); 
    

    which should be

          Elements contentNode = jobNode.select("div.content"); 
    

    Your contentList was null because of this ( as i guessed).

    public class test extends Activity {

    String s;
    List<String> headersList = new ArrayList<String>();
    List<String> contentList = new ArrayList<String>();
    ListView listview;
    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    
    SimpleAdapter simpleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
    
        listview = (ListView) findViewById(R.id.list);
    
        new SampleAsyncTask().execute();
    
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    
    
    
    public class SampleAsyncTask extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(Void... params) {
            try {
                webRequest();
                //setUpSimpleAdapter();
    
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
        }
    
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            List<Map<String, String>> data = new ArrayList<Map<String, String>>();
            for(int i = 0; i < headersList.size(); i++)
            {
                Map<String,String>oglas = new HashMap<String,String>(2);
                oglas.put("naslov", headersList.get(i));
                oglas.put("sadrzaj", contentList.get(i));
                data.add(oglas);
            }
            simpleAdapter = new SimpleAdapter(test.this, data, 
                    android.R.layout.simple_list_item_2,
                    new String[]{"naslov","sadrzaj"},
                    new int[] {android.R.id.text1, android.R.id.text2});
            listview.setAdapter(simpleAdapter);
        }
    
    }
    
    
    public void webRequest() throws Exception{
        String servisURL = "http://www.sczg.unizg.hr/student-servis/";
        String response = Jsoup.connect(servisURL).response().toString();
        System.out.println(response);
        Document doc = Jsoup.connect(servisURL).get();
    
        Elements jobNode = doc.select("div.jobBox");
        Elements headersNode = jobNode.select("h1");
        //Elements contentNode = jobNode.select("content"); 
        Elements contentNode = jobNode.select("div.content"); 
    
        for(int i = 0; i < headersNode.size(); i++){
            headersList.add(headersNode.get(i).text());
        }
        System.out.println("............."+contentNode.size());
        for(int i = 0; i < contentNode.size(); i++){
            contentList.add(contentNode.get(i).text());
    
        }
    }
    }
    

    enter image description here