Thursday, 22 August 2013

Limiting what jsoup retrieves

Limiting what jsoup retrieves

I'm Having fun learning to use jsoup and have successfully retrieved and
displayed data from a website, but now I would like some further guidance
on it if anyone can help.
Using the code below returns all the table rows 30+, How can I retrieve
only say the first 10 of those rows?
also
When returning those rows and the data on them there are gaps/spaces in
the row between the data, the spaces between rows are fine but its the
spaces within the row that I want to get rid of, how can I omit those
spaces/gaps?
My code so far...
package com.example.shiftzer;
import java.io.IOException;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
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").get();
Elements tableRows = doc.select("table.dynlist tr td");
for (Element element : tableRows) {
shippingList = element.text();
arr_shipping.add(shippingList);// add value to
ArrayList
}
} 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);
}
}
}
Thank you.

No comments:

Post a Comment