Reputation: 1503
I am getting a JSON data which contains for example the news headline and the url address for the news in the actual website.
I am using ListView to display the headlines. But how do I make it in such a way that when each item is clicked it will open up the corresponding link in the Android browser. Below is my code snippet:
String title = c.getString(TAG_TITLE);
String link = c.getString(TAG_ALTERNATE);
list.add(title);
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(browserIntent);
}
});
The issue is when I click on the item it loads up the page from only 1 link. All items when clicked go to the same page.
Any idea where I have gone wrong? How do I make it in such a way that each item when clicked will go to it's respective link?
Upvotes: 0
Views: 1150
Reputation:
May be
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(adapter.getItem(arg2).toString());
startActivity(browserIntent);
or Something like this.
However, for understanding :
You get a 1 link because in 2 cases you getting a 1 value. It's simple. Enjoy!:)
Upvotes: 1
Reputation: 1503
Ok got it. I declared another list and get the link from that list. Below is the code:
list.add(title);
linklist.add(link);
final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(linklist.get(arg2).toString()));
startActivity(browserIntent);
}
});
Upvotes: 0
Reputation: 803
i think you should make the title and link into a javabean,so when you click a item ,you can use the javabean and get the link,at last ,you will linked to different websites.
Upvotes: 2
Reputation: 4532
put corresponding link in a list at the time of parsing json.
List<String> listLink= new ArrayList<String>
listLink.add(link);
set crosponding link of title when creation Intent in OnItemClickListner
of ListView
. i.e.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(listLink.get(arg2)));
startActivity(browserIntent);
}
});
Upvotes: 2
Reputation: 3585
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),s[arg2], Toast.LENGTH_LONG).show();
if(s[arg2].equals("Facebook"))
{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.facebook.com")));
}
else if(s[arg2].equals("Twitter"))
{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com")));
}
else
{
//Message here
}
}
});
Upvotes: 1