Guest User

Untitled

a guest
Jan 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. public class PlanetsActivity extends Activity {
  2.  
  3. private ListView mainListView;
  4. private Planet[] planets;
  5. private ArrayAdapter<Planet> listAdapter;
  6.  
  7. /** Called when the activity is first created. */
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12.  
  13. ContentResolver cr = getContentResolver();
  14. Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
  15. null, null, null);
  16.  
  17. // Find the ListView resource.
  18. mainListView = (ListView) findViewById(R.id.mainListView);
  19.  
  20. // When item is tapped, toggle checked properties of CheckBox and
  21. // Planet.
  22. mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  23. @Override
  24. public void onItemClick(AdapterView<?> parent, View item,
  25. int position, long id) {
  26. Planet planet = listAdapter.getItem(position);
  27. planet.toggleChecked();
  28. PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
  29. viewHolder.getCheckBox().setChecked(planet.isChecked());
  30. }
  31. });
  32.  
  33. // Create and populate planets.
  34. planets = (Planet[]) getLastNonConfigurationInstance();
  35. // planets = new Planet[10];
  36. // planets.Add("asdf");
  37. ArrayList<Planet> planetList = new ArrayList<Planet>();
  38.  
  39. if (planets == null) {
  40. if (cur.getCount() > 0) {
  41. planets = new Planet[cur.getCount()] ;
  42. int i=0;
  43. while (cur.moveToNext()) {
  44. String id = cur.getString(cur
  45. .getColumnIndex(ContactsContract.Contacts._ID));
  46. String name = cur.getString(cur
  47. .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  48.  
  49. //Toast.makeText(getApplicationContext(), name,
  50. //Toast.LENGTH_LONG).show();
  51.  
  52. planets = new Planet[] { new Planet(name) };
  53. planetList.addAll(Arrays.asList(planets));
  54.  
  55. //planets[i].setName(name);
  56. //planets[i].setChecked(false)
  57.  
  58. if(Integer.
  59. parseInt(cur
  60. .getString(cur
  61. .getColumnIndex(ContactsContract
  62. .Contacts.HAS_PHONE_NUMBER))) > 0) {
  63. // Query phone here. Covered next
  64. }
  65. i++;
  66. }
  67. }
  68.  
  69. // planets = new Planet[] { new Planet("Mercury"),
  70. // new Planet("Venus"), new Planet("Earth"),
  71. // new Planet("Mars"), new Planet("Jupiter"),
  72. // new Planet("Saturn"), new Planet("Uranus"),
  73. // new Planet("Neptune"), new Planet("Ceres"),
  74. // new Planet("Pluto"), new Planet("Haumea"),
  75. // new Planet("Makemake"), new Planet("Eris") };
  76. }
  77.  
  78.  
  79. // Set our custom array adapter as the ListView's adapter.
  80. listAdapter = new PlanetArrayAdapter(this, planetList);
  81. mainListView.setAdapter(listAdapter);
  82. }
  83.  
  84. /** Holds planet data. */
  85. private static class Planet {
  86. private String name = "";
  87. private boolean checked = false;
  88. public Planet() {}
  89.  
  90. public Planet(String name) {
  91. this.name = name;
  92. }
  93.  
  94. public Planet(String name, boolean checked) {
  95. this.name = name;
  96. this.checked = checked;
  97. }
  98.  
  99. public String getName() {
  100. return name;
  101. }
  102.  
  103. public void setName(String name) {
  104. this.name = name;
  105. }
  106.  
  107. public boolean isChecked() {
  108. return checked;
  109. }
  110.  
  111. public void setChecked(boolean checked) {
  112. this.checked = checked;
  113. }
  114.  
  115. public String toString() {
  116. return name;
  117. }
  118.  
  119. public void toggleChecked() {
  120. checked = !checked;
  121. }
  122. }
  123.  
  124. /** Holds child views for one row. */
  125. private static class PlanetViewHolder {
  126. private CheckBox checkBox;
  127. private TextView textView;
  128.  
  129. public PlanetViewHolder() {
  130. }
  131.  
  132. public PlanetViewHolder(TextView textView, CheckBox checkBox) {
  133. this.checkBox = checkBox;
  134. this.textView = textView;
  135. }
  136.  
  137. public CheckBox getCheckBox() {
  138. return checkBox;
  139. }
  140.  
  141. public void setCheckBox(CheckBox checkBox) {
  142. this.checkBox = checkBox;
  143. }
  144.  
  145. public TextView getTextView() {
  146. return textView;
  147. }
  148.  
  149. public void setTextView(TextView textView) {
  150. this.textView = textView;
  151. }
  152. }
  153.  
  154. /** Custom adapter for displaying an array of Planet objects. */
  155. private static class PlanetArrayAdapter extends ArrayAdapter<Planet> {
  156.  
  157. private LayoutInflater inflater;
  158.  
  159. public PlanetArrayAdapter(Context context, List<Planet> planetList) {
  160. super(context, R.layout.simplerow, R.id.rowTextView, planetList);
  161. // Cache the LayoutInflate to avoid asking for a new one each time.
  162. inflater = LayoutInflater.from(context);
  163. }
  164.  
  165. @Override
  166. public View getView(int position, View convertView, ViewGroup parent) {
  167. // Planet to display
  168. Planet planet = (Planet) this.getItem(position);
  169.  
  170. // The child views in each row.
  171. CheckBox checkBox;
  172. TextView textView;
  173.  
  174. // Create a new row view
  175. if (convertView == null) {
  176. convertView = inflater.inflate(R.layout.simplerow, null);
  177.  
  178. // Find the child views.
  179. textView = (TextView) convertView
  180. .findViewById(R.id.rowTextView);
  181. checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);
  182.  
  183. // Optimization: Tag the row with it's child views, so we don't
  184. // have to
  185. // call findViewById() later when we reuse the row.
  186. convertView.setTag(new PlanetViewHolder(textView, checkBox));
  187.  
  188. // If CheckBox is toggled, update the planet it is tagged with.
  189. checkBox.setOnClickListener(new View.OnClickListener() {
  190. public void onClick(View v) {
  191. CheckBox cb = (CheckBox) v;
  192. Planet planet = (Planet) cb.getTag();
  193. planet.setChecked(cb.isChecked());
  194. }
  195. });
  196. }
  197. // Reuse existing row view
  198. else {
  199. // Because we use a ViewHolder, we avoid having to call
  200. // findViewById().
  201. PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
  202. .getTag();
  203. checkBox = viewHolder.getCheckBox();
  204. textView = viewHolder.getTextView();
  205. }
  206.  
  207. // Tag the CheckBox with the Planet it is displaying, so that we can
  208. // access the planet in onClick() when the CheckBox is toggled.
  209. checkBox.setTag(planet);
  210.  
  211. // Display planet data
  212. checkBox.setChecked(planet.isChecked());
  213. textView.setText(planet.getName());
  214.  
  215. return convertView;
  216. }
  217.  
  218. }
  219.  
  220. public Object onRetainNonConfigurationInstance() {
  221. return planets;
  222. }
  223. }
Add Comment
Please, Sign In to add comment