How to set onItemClick listener with this
I am using this to retreive a list of items and place them in a list. The URL is retrieved also for each item. I am using ListActivity
// 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");
Elements links = doc.getElementsByTag("a"); // or getElementsByClass("b1");
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// ...It
while (postIt.hasNext()) {
// Add the game text to the ArrayList
Element name = postIt.next();
String nameString = name.text();
String platform = postIt.next().text();
Element url = name.select("a").first();
//Here i retreive the url string for each item
String urlString = url.attr("href");
String genre = postIt.next().text();
String releaseDate = postIt.next().text();
gameList.add(new GameRelease(nameString, platform, genre, releaseDate, urlString));
Log.v(TAG, urlString);
}
this.setListAdapter(new GameReleaseAdapter(this, gameList));
}
I created my own ArrayAdapter here...
private class GameReleaseAdapter extends ArrayAdapter<GameRelease> {
priv开发者_开发知识库ate ArrayList<GameRelease> items;
public GameReleaseAdapter(Context context, ArrayList<GameRelease> items) {
// TODO: make a layout for each item which you'd call (for example) itemLayout
super(context, R.layout.item, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO: return an item view styled however you want or as shown in the tutorial
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
GameRelease o = items.get(position);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView plat = (TextView)v.findViewById(R.id.platform);
TextView genre = (TextView)v.findViewById(R.id.genre);
tt.setText(o.getName());
bt.setText("RELEASE DATE: " +o.getReleaseDate());
plat.setText("PLATFORM: " + o.getPlatform());
genre.setText("GENRE: " + o.getGenre());
return v;
}
}
}
How could i store the url for each item so when the item is clicked the url is sent with a extra to another activity? How can i set each item to its own url? How can i let android know that this item in the list was clicked and it should be This url for that particular item?
EDIT - Like this? It gives me tons of syntax errors
while (postIt.hasNext()) {
// Add the game text to the ArrayList
Element name = postIt.next();
String nameString = name.text();
String platform = postIt.next().text();
Element url = name.select("a").first();
String urlString = url.attr("href");
String genre = postIt.next().text();
String releaseDate = postIt.next().text();
gameList.add(new GameRelease(nameString, platform, genre, releaseDate, urlString));
Log.v(TAG, urlString);
}
this.setListAdapter(new GameReleaseAdapter(this, gameList));
final GameReleaseAdapter myGameReleaseAdapter =new GameReleaseAdapter(this,gameList);
setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GameRelease selectedGameRelease =myGameReleaseAdapter .getItem(position);
String urlString=selectedGameRelease.getUrlString();
//Make what you like with the url
}
});
}
when you create the GameReleaseAdapter
in your Activity
, make sure you have a reference to it like
in onCreate
1- read your gameList
2- after reading it, do this line:
final GameReleaseAdapter myGameReleaseAdapter =new GameReleaseAdapter(this,gameList);
3-
getListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GameRelease selectedGameRelease =myGameReleaseAdapter .getItem(position);
String urlString=selectedGameRelease.getUrlString();
//Make what you like with the url
}
});
OnItemClickListener
can be found with AdapterView.OnItemClickListener
精彩评论