Advertisement
Qpel

Untitled

Apr 19th, 2019
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package com.example.sarasas;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.app.ListActivity;
  7. import android.graphics.Color;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.ListView;
  13. import android.widget.Toast;
  14. import android.widget.ArrayAdapter;
  15.  
  16. import java.util.ArrayList;
  17.  
  18. public class MainActivity extends Activity {
  19. ListView listView;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. // Get ListView object from xml
  27. listView = (ListView) findViewById(R.id.list);
  28.  
  29. // Defined Array values to show in ListView
  30. String[] values = new String[] { "Android List View",
  31. "Adapter implementation",
  32. "Simple List View In Android",
  33. "Create List View Android",
  34. "Android Example",
  35. "List View Source Code",
  36. "Solaris",
  37. "List View Array Adapter",
  38. "Android Example List View"
  39. };
  40.  
  41. // Define a new Adapter
  42. // First parameter - Context
  43. // Second parameter - Layout for the row
  44. // Third parameter - ID of the TextView to which the data is written
  45. // Forth - the Array of data
  46.  
  47. //Adapteris adapter = new ArrayAdapter (this, R.layout.eilute, android.R.id.text1, values);
  48.  
  49. Adapteris adapter = new Adapteris(this,values);
  50. // Assign adapter to ListView
  51. listView.setAdapter(adapter);
  52.  
  53. // ListView Item Click Listener
  54. listView.setOnItemClickListener(new OnItemClickListener() {
  55.  
  56. @Override
  57. public void onItemClick(AdapterView<?> parent, View view,
  58. int position, long id) {
  59.  
  60. // ListView Clicked item index
  61. int itemPosition = position;
  62.  
  63. // ListView Clicked item value
  64. String itemValue = (String) listView.getItemAtPosition(position);
  65.  
  66. // Show Alert
  67. Toast.makeText(getApplicationContext(),
  68. "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
  69. .show();
  70.  
  71. }
  72.  
  73. });
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83. }
  84.  
  85.  
  86.  
  87. package com.example.sarasas;
  88.  
  89. import android.content.Context;
  90. import android.view.LayoutInflater;
  91. import android.view.View;
  92. import android.view.ViewGroup;
  93. import android.widget.ArrayAdapter;
  94. import android.widget.ImageView;
  95. import android.widget.TextView;
  96.  
  97. public class Adapteris extends ArrayAdapter {
  98. private final Context context;
  99. private final String[] values;
  100.  
  101. public Adapteris (Context context, String[] values) {
  102. super(context, R.layout.eilute, values);
  103. this.context = context;
  104. this.values = values;
  105. }
  106.  
  107. @Override
  108. public View getView(int position, View convertView, ViewGroup parent) {
  109. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  110. View rowView = inflater.inflate(R.layout.eilute, parent, false);
  111. TextView textView = (TextView) rowView.findViewById(R.id.label);
  112. ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
  113. textView.setText(values[position]);
  114. // Change the icon for Windows and iPhone
  115. String s = values[position];
  116. if (s.startsWith("Android Example") || s.startsWith("Android List View")
  117. || s.startsWith("Solaris")) {
  118. imageView.setImageResource(R.drawable.no);
  119. } else {
  120. imageView.setImageResource(R.drawable.ok);
  121. }
  122.  
  123. return rowView;
  124. }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. <?xml version="1.0" encoding="utf-8"?>
  133. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  134. xmlns:app="http://schemas.android.com/apk/res-auto"
  135. android:layout_width="match_parent"
  136. android:layout_height="match_parent">
  137.  
  138. <ImageView
  139. android:id="@+id/icon"
  140. android:layout_width="wrap_content"
  141. android:layout_height="wrap_content"
  142. android:layout_weight="1"
  143. app:srcCompat="@android:drawable/btn_star_big_on" />
  144.  
  145. <TextView
  146. android:id="@+id/label"
  147. android:layout_width="wrap_content"
  148. android:layout_height="wrap_content"
  149. android:layout_weight="1"
  150. android:text="@+id/label"
  151. android:textSize="40px"/>
  152. </LinearLayout>
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. <?xml version="1.0" encoding="utf-8"?>
  160. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  161. xmlns:app="http://schemas.android.com/apk/res-auto"
  162. xmlns:tools="http://schemas.android.com/tools"
  163. android:layout_width="match_parent"
  164. android:layout_height="match_parent"
  165. tools:context=".MainActivity">
  166.  
  167. <ListView
  168. android:id="@+id/list"
  169. android:layout_width="395dp"
  170. android:layout_height="715dp"
  171. tools:layout_editor_absoluteX="16dp" />
  172. </android.support.constraint.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement