1

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());
            }



        }

    }
4

1 回答 1

1

不确定这是否是最好的方法。我已经修改了上面的内容。您可以参考以下内容。如果列表视图中的数据正确,请检查快照。(因为我不理解文本数据)。如果列表中的数据不正确,请根据您的要求修改以下内容。

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());
    }
}
}

编辑:

问题是这个

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

应该是

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

因此,您的 contentList 为空(正如我所猜测的那样)。

公共类测试扩展活动{

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());

    }
}
}

在此处输入图像描述

于 2013-06-04T12:20:15.567 回答