Jsoup和Listview编程

时间:2014-08-17 06:11:19

标签: android jsoup

Java代码

private ListView listView;
private ArrayList<String> arrayList;
private ArrayAdapter<String> adapter;

public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);   

  setContentView(R.layout.announcement);

  Document document = Jsoup.parse("url");
  String element = document.select("tbody").select("div.m_ltitle").text();

  arrayList = new ArrayList<String>();
  arrayList.add(element);


  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);


  listView = (ListView)findViewById(R.id.announcementlist);
  listView.setAdapter(adapter);
  listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


  listView.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

          String str = (String)adapter.getItem(position);
          Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();

      }
  });
  }
}

“嗯,我真的是一个关于android的新手”(抱歉质量低劣的问题), 并使用Jsoup进行解析Activity。 我通过按标签选择代码来解析文本,然后添加到位于R.layout.announcement(listview id:announcementlist)上的listview。 然而,它重复了应用程序已停止。

任何建议都会感激不尽。

这些是logcat

08-17 17:16:58.383:D / AndroidRuntime(10404):关闭虚拟机

08-17 17:16:58.393:W / dalvikvm(10404):threadid = 1:线程退出时未捕获异常(group = 0x419efce0)

08-17 17:16:58.393:E / AndroidRuntime(10404):致命异常:主

08-17 17:16:58.393:E / AndroidRuntime(10404):处理:com.example,PID:10404

08-17 17:16:58.393:E / AndroidRuntime(10404):android.app.Fragment $ InstantiationException:尝试实例化一个不是片段的类com.example.Announcement

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.app.Fragment.instantiate(Fragment.java:585)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.app.Fragment.instantiate(Fragment.java:561)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.example.MainActivity $ ProductTabListener.onTabSelected(MainActivity.java:98)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.android.internal.app.ActionBarImpl $ TabImpl.select(ActionBarImpl.java:1105)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.android.internal.widget.ScrollingTabContainerView $ TabClickListener.onClick(ScrollingTabContainerView.java:547)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.view.View.performClick(View.java:4445)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.view.View $ PerformClick.run(View.java:18446)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.os.Handler.handleCallback(Handler.java:733)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.os.Handler.dispatchMessage(Handler.java:95)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.os.Looper.loop(Looper.java:136)

08-17 17:16:58.393:E / AndroidRuntime(10404):在android.app.ActivityThread.main(ActivityThread.java:5139)

08-17 17:16:58.393:E / AndroidRuntime(10404):at java.lang.reflect.Method.invokeNative(Native Method)

08-17 17:16:58.393:E / AndroidRuntime(10404):at java.lang.reflect.Method.invoke(Method.java:515)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:796)

08-17 17:16:58.393:E / AndroidRuntime(10404):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)

08-17 17:16:58.393:E / AndroidRuntime(10404):at dalvik.system.NativeStart.main(Native Method)

08-17 17:16:58.393:E / AndroidRuntime(10404):引起:java.lang.ClassCastException

08-17 17:16:58.393:E / AndroidRuntime(10404):... 17更多

最后一个控制台

在设备76250880上启动活动com.example.MainActivity

ActivityManager:开始:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example / .MainActivity}

1 个答案:

答案 0 :(得分:0)

尝试使用String数组:

String [] values = new String [1]; //根据需要调整大小

String element = document.select(“tbody”)。select(“div.m_ltitle”)。text();

值[0] =元素;

ArrayAdapter adapter = new ArrayAdapter (这,android.R.layout.layout.simple_list_item_1,值);

listView =(ListView)findViewById(R.id.announcementlist);

listView.setAdapter(适配器);