使用jsoup删除空表数据单元格

时间:2013-08-26 18:54:04

标签: android html parsing listview jsoup

有没有办法使用jsoup从结果中删除表格单元格,如果它们是空的? 我下面的代码很好地收集了信息但是当我把它放到listview中时,空单元格显示为列表视图的空行, 也 如何在一定数量的行之后将空行插入列表视图?

我知道......首先我不想要空行然后我想指定我想要空行的位置。

...代码

 public class MainActivity extends Activity{

TextView textView1;
ListView shippingList; 

  public static final String APP_PREFERENCES = "AppPrefs";
    SharedPreferences settings; 
    SharedPreferences.Editor prefEditor;

   @Override
     public void onCreate(Bundle savedInstanceState) {         
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main_activity);
        //rest of the code

       textView1 = (TextView)findViewById(R.id.textView1);
       shippingList = (ListView) findViewById(R.id.listView1);

       settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
       prefEditor = settings.edit();

       new VTSTask().execute();//starts AsyncTask in private class VTSTask to get shipping info
    }

   private class VTSTask extends AsyncTask<Void, Void, ArrayList<String>> {
       ArrayList<String> arr_shipping=new ArrayList<String>();
        /**
         * @param args
         */
        @Override
        protected ArrayList<String>  doInBackground(Void... params) {

            Document doc;
            String shippingList;

            try {
                doc = Jsoup.connect("https://vts.mhpa.co.uk/main_movelistb.asp").timeout(600000).get(); 
                //Elements tableRows = doc.select("table.dynlist tr");
              //  Elements tableRows = doc.select("table.dynlist tr:not(:eq(0))td:eq(0),td:eq(1),td:eq(2)");//removes Table Rows
                Elements tableRows = doc.select("table.dynlist td:eq(0),td:eq(1),td:eq(3),td:eq(4),td:eq(5),td:eq(7),td:eq(8)");
                tableRows.size();
                    for(int i = 1; i < 80; i++){//only allows x results from vts list, from 1 not 0. 0 produces needless results
                      shippingList = tableRows.get(i).text() +"\n";

                      arr_shipping.add(shippingList);// add value to ArrayList
                      System.out.println(shippingList);
                    } 
                 } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }      

            return arr_shipping;//<< return ArrayList from here
        }

         @Override
         protected void onPostExecute(ArrayList<String> result) {        
             //TextView tVShipping=(TextView)findViewById(R.id.textView2);

             shippingList = (ListView) findViewById(R.id.listView1);
             ArrayAdapter<String> adapter = 
                 new ArrayAdapter<String>(MainActivity.this, 
                                          android.R.layout.simple_list_item_1, 
                                          android.R.id.text1);

             for (String shipping_result : result)
             {
                adapter.add(shipping_result);
             }

             // Assign adapter to ListView
             shippingList.setAdapter(adapter); 

          }
    }


} 

我正在解析的网站摘录......

Screen Capture of html

在第6行 td 行只有空格,有时候它有信息,有时候没有,所以如果那个单元格中什么都没有,可以过滤掉吗?

由于

1 个答案:

答案 0 :(得分:1)

如果你只关心6 th <td>

,这应该可以胜任你的工作
Element element = doc.select("table.dynlist td:eq(5)").first();

    if(element!= null && text != null && text.replaceAll("&nbsp;| ", "").isEmpty())
    {
        element.remove();
    }