Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. private ListView lv;
  4.  
  5. ///////////
  6.  
  7.  
  8. ///////////////////////
  9. // Listview Adapter
  10. ArrayAdapter<String> adapter;
  11.  
  12. // Search EditText
  13. EditText inputSearch;
  14.  
  15.  
  16. // ArrayList for Listview
  17. ArrayList<HashMap<String, String>> productList;
  18.  
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. // Listview Data
  25. String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
  26. "iPhone 4S", "Samsung Galaxy Note 800",
  27. "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};
  28.  
  29. lv = (ListView) findViewById(R.id.list_view);
  30. inputSearch = (EditText) findViewById(R.id.inputSearch);
  31.  
  32. // Adding items to listview
  33. adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
  34. lv.setAdapter(adapter);
  35.  
  36.  
  37.  
  38. inputSearch.addTextChangedListener(new TextWatcher() {
  39.  
  40. @Override
  41. public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
  42. // When user changed the Text
  43. MainActivity.this.adapter.getFilter().filter(cs);
  44. }
  45.  
  46. @Override
  47. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  48. int arg3) {
  49. // TODO Auto-generated method stub
  50.  
  51. }
  52.  
  53. @Override
  54. public void afterTextChanged(Editable arg0) {
  55. // TODO Auto-generated method stub
  56. }
  57. });
  58.  
  59. }
  60. AdapterView.OnItemClickListener adapterClick = new AdapterView.OnItemClickListener() {
  61. @Override
  62. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  63. //要執行的事情
  64. ListView lvMoogle5 = (ListView)findViewById(R.id.list_item);
  65. lvMoogle5.setOnItemClickListener(adapterClick);
  66. String[] links = getResources().getStringArray(R.array.link);
  67.  
  68. Uri uri = Uri.parse(links[position]);
  69. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  70. startActivity(intent);
  71. }
  72. };
  73. }
  74.  
  75. <resources>
  76. <string name="app_name">Listview</string>
  77.  
  78. <string name="hello_world">Hello world!</string>
  79. <string name="action_settings">Settings</string>
  80. <string-array name="link">
  81. <item>http://finalfantasy.wikia.com/wiki/Sephiroth_(Crisis_Core_Boss)</item>
  82. <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
  83. <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
  84. <item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
  85. <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
  86. <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
  87. <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item><item>http://finalfantasy.wikia.com/wiki/Kefka_Palazzo</item>
  88. <item>http://finalfantasy.wikia.com/wiki/Exdeath_(Boss)</item>
  89. </string-array>
  90.  
  91.  
  92. </resources>
  93.  
  94. lv = (ListView) findViewById(R.id.list_view);
  95. lv.setOnItemClickListener(new OnItemClickListener() {
  96.  
  97. @Override
  98. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  99. String[] links = getResources().getStringArray(R.array.link);
  100. Uri uri = Uri.parse(links[position]);
  101. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  102. startActivity(intent);
  103.  
  104. }
  105. });
  106.  
  107. lv.setOnItemClickListener(new OnItemClickListener() {
  108. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  109. Object obj = lv.getAdapter().getItem(position);
  110. Uri uri = Uri.parse(obj.toString());
  111. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  112. startActivity(intent);
  113. }
  114. });
  115.  
  116. public class MainActivity extends Activity {
  117.  
  118.  
  119. String[] web = {
  120. "Yahoo",
  121. "Google",
  122. "Facebook"
  123. };
  124. @Override
  125. protected void onCreate(Bundle savedInstanceState) {
  126. super.onCreate(savedInstanceState);
  127. setContentView(R.layout.activity_mainlist);
  128.  
  129. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
  130. R.layout.activity_listview, web);
  131.  
  132. ListView list = (ListView) findViewById(R.id.list);
  133. list.setAdapter(adapter);
  134.  
  135. list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  136. @Override
  137. public void onItemClick(AdapterView<?> parent, View view,
  138. int position, long id) {
  139. switch(position){
  140. case 0: String url = "yahoo.com";
  141.  
  142. Intent i = new Intent(Intent.ACTION_VIEW);
  143. i.setData(Uri.parse(url));
  144. startActivity(i);
  145. break;
  146.  
  147.  
  148. }
  149.  
  150. }
  151. @SuppressWarnings("unused")
  152. public void onClick(View v){
  153. };
  154. });
  155.  
  156. }//end oncreate
  157.  
  158.  
  159. }//endactivity
  160.  
  161. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  162.  
  163.  
  164. @Override
  165. public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
  166. Bundle bn = new Bundle();
  167.  
  168.  
  169. LinkModel model = (LinkModel) parent.getItemAtPosition(position);
  170.  
  171.  
  172. String a = model.getLink ();
  173.  
  174. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(a));
  175. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  176. startActivity ( intent );
  177. bn.putInt("pos", position);
  178. intent.putExtras(bn);
  179.  
  180. }
  181. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement