Skip to content

Instantly share code, notes, and snippets.

@shobhitchittora
Last active August 29, 2015 14:06
ListView & Intent
package com.roadtodroid.listviewintent;
import java.util.Arrays;
import java.util.List;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayAdapter<String> arrayadapter;
private List<String> movie_names = Arrays.asList("Tomorrow Never Dies",
"Quantum of Solace",
"Die Another Day",
"Casino Royale",
"Live and Let Die",
"For Your Eyes Only",
"Skyfall",
"You Only Live Twice",
"The World Is Not Enough",
"Dr. No",
"From Russia with Love"); //initialize a list object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayadapter= new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1);
show_in_list();
setListAdapter(arrayadapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Searching for: "+movie_names.get(position), Toast.LENGTH_LONG).show();
String searchquery=movie_names.get(position);
Intent my_intent = new Intent(Intent.ACTION_WEB_SEARCH); //Intent for launching the browser
my_intent.putExtra(SearchManager.QUERY, searchquery); //adding searchquery to intent
startActivity(my_intent); // starting the activity
}
public void show_in_list(){ // called to set list view
for(String s : movie_names )
arrayadapter.add(s); //adding strings to arrayadapter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment