0

Is there a way using jsoup to remove table cells from the results if they are empty? The code I have below gathers the information nicely but when I put it into a listview the empty cells show up as empty rows of the list view, also how can I insert a empty row into the list view after a certain amount of rows?

I know...first I don't want empty rows then I want to specify where I want empty rows.

Code...

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

          }
    }


} 

snippet from website that I'm parsing...

Screen Capture of html

On the 6th td line there is just blank space, sometimes this has information in it sometimes not, so is it possible to filter it out if there is nothing in that cell??

Thanks

4

1 回答 1

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();
    }
于 2013-08-27T12:49:32.513 回答