Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends Activity {
- private ListView lv;
- ///////////
- ///////////////////////
- // Listview Adapter
- ArrayAdapter<String> adapter;
- // Search EditText
- EditText inputSearch;
- // ArrayList for Listview
- ArrayList<HashMap<String, String>> productList;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // Listview Data
- String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
- "iPhone 4S", "Samsung Galaxy Note 800",
- "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
- lv = (ListView) findViewById(R.id.list_view);
- inputSearch = (EditText) findViewById(R.id.inputSearch);
- // Adding items to listview
- adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
- lv.setAdapter(adapter);
- inputSearch.addTextChangedListener(new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
- // When user changed the Text
- MainActivity.this.adapter.getFilter().filter(cs);
- }
- @Override
- public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
- int arg3) {
- // TODO Auto-generated method stub
- }
- @Override
- public void afterTextChanged(Editable arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- AdapterView.OnItemClickListener adapterClick = new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- //要執行的事情
- ListView lvMoogle5 = (ListView)findViewById(R.id.list_item);
- lvMoogle5.setOnItemClickListener(adapterClick);
- String[] links = getResources().getStringArray(R.array.link);
- Uri uri = Uri.parse(links[position]);
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- };
- }
- <resources>
- <string name="app_name">Listview</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string-array name="link">
- <item>http://finalfantasy.wikia.com/wiki/Sephiroth_(Crisis_Core_Boss)</item>
- <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
- <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
- <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
- <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
- <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
- <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
- <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
- </string-array>
- </resources>
- lv = (ListView) findViewById(R.id.list_view);
- lv.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- String[] links = getResources().getStringArray(R.array.link);
- Uri uri = Uri.parse(links[position]);
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- });
- lv.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Object obj = lv.getAdapter().getItem(position);
- Uri uri = Uri.parse(obj.toString());
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
- }
- });
- public class MainActivity extends Activity {
- String[] web = {
- "Yahoo",
- "Google",
- "Facebook"
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_mainlist);
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- R.layout.activity_listview, web);
- ListView list = (ListView) findViewById(R.id.list);
- list.setAdapter(adapter);
- list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- switch(position){
- case 0: String url = "yahoo.com";
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- break;
- }
- }
- @SuppressWarnings("unused")
- public void onClick(View v){
- };
- });
- }//end oncreate
- }//endactivity
- lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
- Bundle bn = new Bundle();
- LinkModel model = (LinkModel) parent.getItemAtPosition(position);
- String a = model.getLink ();
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(a));
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity ( intent );
- bn.putInt("pos", position);
- intent.putExtras(bn);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement