1

I am using this to get a list of elements from a webpage. http://www.gamespy.com/index/release.html

// Get all the game elements
Elements games = doc.select("div.FP_Up_TextWrap b");

 // Create new ArrayList
ArrayList<String> gameList = new ArrayList<String>();

// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();

while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}

This returns all the tags with which is what I want. But it returns the full tag with the title I just want the title the release date and a Img sr. I would like to return it to a list view. How would I go about doing this? I maybe doing it totally wrong so you guys may want to check out the HTML page source.

EDIT - Gives me NullPointer error

  /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    outputTextView = (TextView)findViewById(R.id.outputTextView);

   ListView list = (ListView)findViewById(R.id.list);
   ArrayList<String> gameList = new ArrayList<String>();


      Document doc = null;
    try {
        doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        // Get all td's that are a child of a row - each game has 4 of these
        Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
        // Iterator over those elements     
        ListIterator<Element> postIt = games.listIterator();          
        while (postIt.hasNext()) {     
             // Add the game text to the ArrayList     
             gameList.add(postIt.next().text());     
        }         

        String[] items = new String[gameList.size()];
        gameList.toArray(items);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);

    //  error points here    
             list.setAdapter(adapter);
    }


    }

EDIT - Layout for the list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/outputTextView"
/>
<ListView    android:layout_height="wrap_content" android:id="@+id/list" android:layout_width="match_parent"></ListView>
</LinearLayout>
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118

2 Answers2

1

You won't be able to "return a list view" using this code, but you could create your own list view sub class which you could then return, following the same techniques:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView listView1 = (ListView) findViewById(R.id.listView1);

    ArrayList<String> gameList = new ArrayList<String>();

    // Iterator over those elements     
    ListIterator<Element> postIt = games.listIterator();          
    while (postIt.hasNext()) {     
         // Add the game text to the ArrayList     
         gameList.add(postIt.next().text());     
    }         

    String[] items = new String[gameList.size()];
    gameList.toArray(items);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.some_list_item_view, items);

    listView1.setAdapter(adapter);
}

Ultimately what it comes down to is setting the list adapter for the list. If you need to create your own list adapter you can subclass and implement ListAdapter to databind other object types, but there's already a predefined adapter (ArrayAdapter) for handling simple string sets for ListView/ListActivity databinding.

UPDATE - Poster requested additional implementation details:

Given:

public class GameMeta {
    private String m_title;
    private java.util.Date m_releaseDate;

    public GameMeta() {
    }

    public String getTitle(){
         return m_title;
    }

    public void setTitle(String value) {
         m_title = value;
    }

    public java.util.Date getReleaseDate(){
         return m_releaseDate;
    }

    public void setReleaseDate(java.util.Date releaseDate){
         m_releaseDate = releaseDate;
    }
}

You might create the adapter:

public class GamesMetaAdapter implements Adapter extends ListAdapter {
     private ArrayList<GameMeta> m_list = new ArrayList<GameMeta>();
     private Context m_context = null;

     public GamesMetaAdapter(Context context) {
         m_context = context;
     }

     @Override
     public int getCount(){
          return m_list.size();
     }

     @Override
     public Object getItem(int position){
        if(position < m_list.size()){
          return m_list.getAt(position);
        }
        return null;
     }

     @Override
     public long getItemId(int position) {
        return (long)position;
     }

     @Override
     public int getItemViewType(int position) {
        return IGNORE_ITEM_VIEW_TYPE;
     }

     @Override
     public View getView (int position, View convertView, ViewGroup parent) {
             TextView simpleView = new TextView(this.m_context);
             String viewText = this.m_items[position].getTitle() + ", Released " + this.m_items[position].getReleaseDate().toString();

             simpleView.setText(this.m_items[position].getTitle());

             if(TextView.class.isInstance(convertView)) {
                   convertView = simpleView;
             }

             return simpleView;
     }


     @Override
     public int getViewTypeCount() {
        return 1;
     }

     @Override
     public boolean hasStableIds(){
        return false;
     }

     @Override
     public boolean isEmpty() {
        return m_list.size() == 0;
     }

     @Override
     public void registerDataSetObserver (DataSetObserver observer) {

     }

     @Override
     public void unregisterDataSetObserver (DataSetObserver observer) {

     }

     /* LIST ADAPTER MEMBERS */
     @Override
     public boolean areAllItemsEnabled() {
          return true;
     }

     @Override
     public boolean isEnabled(int position) {
        return true;
     }    
}

I don't have a Java compiler here, so please don't ding me if this doesn't just copy/paste right into your project, but it should be well north of 90% there.

Happy coding.

B

Brian
  • 2,772
  • 15
  • 12
  • could you put the technique I need to use to get the game titles and the release dates? For each to be in the element games? The code above doesn't match with the webpage it was from one of my old projects – coder_For_Life22 Aug 15 '11 at 15:22
  • Do i do all of this in one class? Or make seperate classes? And how does all of this tie together? – coder_For_Life22 Aug 15 '11 at 17:23
  • You can put the adapter in one class if you'll reuse it, or you can make it a private child class of your ListView class. Entirely specific to code style/standards and any reuse you anticipate for the adapter/view. – Brian Aug 15 '11 at 17:27
  • Im a little confused with the code above.. Which part do i need to get it to work? And is the rest extra? – coder_For_Life22 Aug 15 '11 at 17:29
  • @coder_For_Life22 - short of writing your software for you there's not much more I can help you with here. The code i've provided you is what you need to make your own list adapter. A list adapter is the component that your list uses under the hood to bind data to elements - elements, in the android world, are Views. This component translates data into views, by row number, for lists. Best I can do at this point is just point you in the right direction - here's a sample implementation: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html. – Brian Aug 15 '11 at 17:34
  • @coder_For_Life22 - you're killing me. The first words of my answer are: "You won't be able to "return a list view" using this code, but you could...". If you want to returns something, create a class that creates a list view, creates the adapter, sets the lists' adapter to the adapter for game data, and return the list view. Seriously man. The site isn't called writemycodeforme.com. Maybe you need to hire a contractor. – Brian Aug 15 '11 at 17:42
  • Yeah i know im sorry dude..Hey but thats a great idea! a website called writemycodeforme.com where people hire others to write there code for them. But check out my edit. Im getting it to return the titles and the release date with the edit above. – coder_For_Life22 Aug 15 '11 at 17:45
  • LOL - they have one - it's called dice.com :) – Brian Aug 15 '11 at 17:48
  • Okay so now it looks like your adapter or your view is undefined. Maybe set a breakpoint and find out which? If your adapter is undefined, make sure you've actually defined a layout for simple_list_item_1 – Brian Aug 15 '11 at 17:54
  • Maybe me and yu can come up with a better one. Did yu see my edit above – coder_For_Life22 Aug 15 '11 at 17:55
  • Hopefully you figured it out - looks like you had to replace the simple_list_item with output item... sorry for getting snappy earlier - hope this was a success for you. – Brian Aug 15 '11 at 22:47
1

This should gather the titles and release dates. Which image did you also want?

// Get all td's that are a child of a row - each game has 4 of these
Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");

// Iterator over those elements
ListIterator<Element> gameIt = games.listIterator();

while (gameIt.hasNext()) {
    // Get the title of the game
    Element title = gameIt.next();

    System.out.println(title.text());

    // Unneeded elements
    Element platform = gameIt.next();
    Element genre = gameIt.next();

    // Get the release date of the game
    Element release = gameIt.next();
    System.out.println(release.text() + "\n@@@@@@");
}
Aaron Foltz
  • 138
  • 2
  • 9