今天我问过类似的问题,问题出在Array适配器上。 我现在尝试使用简单适配器设置列表视图,因为我需要标题行和子内容。
程序启动时没有错误,但屏幕为空白。
编辑:我已经更新了代码,它现在运行没有问题。我认为这个问题出现在对于contentNode的Jsoup查询中。以下是代码:
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());
}
}
}
答案 0 :(得分: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为空(正如我猜测的那样)。
公共类测试扩展了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());
}
}
}