Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.90 KB | None | 0 0
  1. ListView list;
  2. private ContactsList[] friends;
  3. private ArrayAdapter<ContactsList> listAdapter;
  4.  
  5. final Context context = this;
  6.  
  7. Cursor cursor;
  8.  
  9. String[] buddiesList =
  10. {"Kanak Priya",
  11. "Joanne Liew",
  12. "Michelle Lam",
  13. "Teo Kin Hua",
  14. "Melissa Haiting",
  15. "David Yeo",
  16. "Natasha Akhbar",
  17. "Gillian Gan",
  18. "Sonia",
  19. "Ms Long",
  20. "Joan Tang",
  21. "Stephanie",
  22. "Nur Ashiqin"
  23. };
  24.  
  25. BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);
  26.  
  27. /** Called when the activity is first created. */
  28. @Override
  29. public void onCreate(Bundle savedInstanceState)
  30. {
  31. super.onCreate(savedInstanceState);
  32. //setListAdapter(new MyCustomAdapter(ContactsList.this, R.layout.contacts_list, buddiesList));
  33. setContentView(R.layout.contacts_list);
  34.  
  35. setListAdapter(new ArrayAdapter<String>(this, R.layout.contacts_list, buddiesList));
  36.  
  37. ListView list = (ListView) findViewById(android.R.id.list);
  38. //ListView list = getListView();
  39. list.setOnItemClickListener(new AdapterView.OnItemClickListener()
  40. {
  41.  
  42. @Override
  43. public void onItemClick(AdapterView<?> parent, View view, int position,
  44. long id)
  45. {
  46. ContactsList friends = listAdapter.getItem(position);
  47. friends.**toggleChecked**();
  48. ContactsListViewHolder viewHolder = (ContactsListViewHolder) view.getTag();
  49. viewHolder.**getCheckBox**().setChecked(friends.**isChecked**());
  50.  
  51.  
  52. Cursor cursor = null;
  53.  
  54. cursor = (Cursor) parent.getItemAtPosition(position);
  55. Intent intent = new Intent(ContactsList.this, Create_Events.class);
  56. intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
  57. startActivity(intent);
  58.  
  59. }
  60.  
  61. });
  62.  
  63. ContactsList[] arrContacts = friendsList.toArray(new ContactsList[0]);
  64.  
  65.  
  66. Uri allContacts = Uri.parse("content://contacts/people");
  67.  
  68. Cursor c = managedQuery(allContacts, null, null, null, null);
  69.  
  70. String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
  71. int[] views = new int[] {R.id.contactName};
  72.  
  73. startManagingCursor(c);
  74.  
  75. SimpleCursorAdapter friendsAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
  76. this.setListAdapter(friendsAdapter);
  77. }
  78.  
  79. private static class ContactsName
  80. {
  81. private String name = "";
  82. private boolean checked = false;
  83.  
  84. public ContactsName(String name)
  85. {
  86. this.name = name;
  87. }
  88.  
  89. public String getName()
  90. {
  91. return name;
  92. }
  93.  
  94. public boolean isChecked()
  95. {
  96. return checked;
  97. }
  98.  
  99. public void setChecked(boolean checked)
  100. {
  101. this.checked = checked;
  102. }
  103.  
  104. public String toString()
  105. {
  106. return name;
  107. }
  108.  
  109. public void toggleChecked()
  110. {
  111. checked = !checked;
  112. }
  113. }
  114.  
  115. private static class ContactsListViewHolder
  116. {
  117. private CheckBox nameCheck;
  118. private TextView contactName;
  119.  
  120. public ContactsListViewHolder(TextView contactName, CheckBox nameCheck)
  121. {
  122. this.nameCheck = nameCheck;
  123. this.contactName = contactName;
  124. }
  125.  
  126. public CheckBox getNameCheck()
  127. {
  128. return nameCheck;
  129. }
  130.  
  131. public TextView getContactName()
  132. {
  133. return contactName;
  134. }
  135. }
  136.  
  137. private static class ContactsCustomAdapter extends ArrayAdapter<ContactsList>
  138. {
  139. private LayoutInflater inflater;
  140.  
  141. public ContactsCustomAdapter(Context context, List<ContactsList> friendsList)
  142. {
  143. **super(context, R.layout.contacts_list, R.id.contactName, R.id.contactCheckbox, friendsList);**
  144. inflater = LayoutInflater.from(context);
  145. }
  146.  
  147. @Override
  148. public View getView(int position, View convertView, ViewGroup parent)
  149. {
  150. ContactsList friends = (ContactsList) this.getItem(position);
  151.  
  152. CheckBox nameCheck;
  153. TextView contactName;
  154.  
  155. if(convertView == null)
  156. {
  157. convertView = inflater.inflate(R.layout.contacts_list, null);
  158.  
  159. contactName = (TextView) convertView.findViewById(R.id.contactName);
  160. nameCheck = (CheckBox) convertView.findViewById(R.id.contactCheckbox);
  161.  
  162. convertView.setTag(new ContactsListViewHolder(contactName, nameCheck));
  163.  
  164. nameCheck.setOnClickListener(new View.OnClickListener()
  165. {
  166.  
  167. @Override
  168. public void onClick(View v)
  169. {
  170. CheckBox cb = (CheckBox) v;
  171. ContactsList friends = (ContactsList) cb.getTag();
  172. friends.**setChecked**(cb.isChecked());
  173.  
  174. if(cb.isChecked())
  175. {
  176. friendsList.add(friends.**name**);
  177. }
  178. else
  179. {
  180. friendsList.remove(friends.**name**);
  181. }
  182. }
  183. });
  184. }
  185. else
  186. {
  187. ContactsListViewHolder viewHolder = (ContactsListViewHolder) convertView.getTag();
  188. nameCheck = viewHolder.**getCheckBox**();
  189. contactName = viewHolder.**getTexView**();
  190. }
  191. }
  192. }
  193.  
  194. @Override
  195. public void onListItemClick(ListView l, View v, int position, long id)
  196. {
  197. buddyDB.open();
  198. long name_id;
  199. super.onListItemClick(l, v, position, id);
  200.  
  201. Cursor cursor = null;
  202.  
  203. cursor = (Cursor) l.getItemAtPosition(position);
  204. Intent intent = new Intent(ContactsList.this, Create_Events.class);
  205. intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
  206. startActivity(intent);
  207.  
  208. ListView list = getListView();
  209. list.setChoiceMode(2);
  210. list.setTextFilterEnabled(true);
  211.  
  212. l.setItemChecked(position, l.isItemChecked(position));
  213.  
  214. Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
  215. c.moveToPosition(position);
  216.  
  217. TextView contactName = (TextView) v.findViewById(R.id.contactName);
  218. String NameValue = contactName.getText().toString();
  219. name_id = buddyDB.insertNames(NameValue);
  220.  
  221. Toast.makeText(getBaseContext(),
  222. "Selected: " + buddiesList[position], Toast.LENGTH_SHORT).show();
  223. buddyDB.close();
  224.  
  225.  
  226.  
  227. }
  228.  
  229. private static class ContactsName
  230. {
  231. private String name = "";
  232.  
  233. public class PlanetsActivity extends Activity {
  234. static ArrayList<String> FavList = new ArrayList<String>();
  235.  
  236. private ListView mainListView ;
  237. private Planet[] planets ;
  238. private ArrayAdapter<Planet> listAdapter ;
  239.  
  240. /** Called when the activity is first created. */
  241. @Override
  242. public void onCreate(Bundle savedInstanceState) {
  243. super.onCreate(savedInstanceState);
  244. setContentView(R.layout.main2);
  245.  
  246. // Find the ListView resource.
  247. mainListView = (ListView) findViewById( R.id.mainListView );
  248.  
  249. // When item is tapped, toggle checked properties of CheckBox and Planet.
  250. mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  251. @Override
  252. public void onItemClick( AdapterView<?> parent, View item,
  253. int position, long id) {
  254. Planet planet = listAdapter.getItem( position );
  255. planet.toggleChecked();
  256. PlanetViewHolder viewHolder = (PlanetViewHolder) item.getTag();
  257. viewHolder.getCheckBox().setChecked( planet.isChecked() );
  258. }
  259. });
  260.  
  261.  
  262. // Create and populate planets.
  263. planets = (Planet[]) getLastNonConfigurationInstance() ;
  264. if ( planets == null ) {
  265. planets = new Planet[] {
  266. new Planet("Mercury"), new Planet("Venus"), new Planet("Earth"),
  267. new Planet("Mars"), new Planet("Jupiter"), new Planet("Saturn"),
  268. new Planet("Uranus"), new Planet("Neptune"), new Planet("Ceres"),
  269. new Planet("Pluto"), new Planet("Haumea"), new Planet("Makemake"),
  270. new Planet("Eris")
  271. };
  272. }
  273. ArrayList<Planet> planetList = new ArrayList<Planet>();
  274. planetList.addAll( Arrays.asList(planets) );
  275.  
  276. // Set our custom array adapter as the ListView's adapter.
  277. listAdapter = new PlanetArrayAdapter(this, planetList);
  278. mainListView.setAdapter( listAdapter );
  279. }
  280.  
  281. /** Holds planet data. */
  282. private static class Planet {
  283. private String name = "" ;
  284. private boolean checked = false ;
  285. public Planet( String name ) {
  286. this.name = name ;
  287. }
  288. public String getName() {
  289. return name;
  290. }
  291. public boolean isChecked() {
  292. return checked;
  293. }
  294. public void setChecked(boolean checked) {
  295. this.checked = checked;
  296. }
  297. public String toString() {
  298. return name ;
  299. }
  300. public void toggleChecked() {
  301. checked = !checked ;
  302. }
  303. }
  304.  
  305. /** Holds child views for one row. */
  306. private static class PlanetViewHolder {
  307. private CheckBox checkBox ;
  308. private TextView textView ;
  309. public PlanetViewHolder( TextView textView, CheckBox checkBox ) {
  310. this.checkBox = checkBox ;
  311. this.textView = textView ;
  312. }
  313. public CheckBox getCheckBox() {
  314. return checkBox;
  315. }
  316. public TextView getTextView() {
  317. return textView;
  318. }
  319. }
  320.  
  321. /** Custom adapter for displaying an array of Planet objects. */
  322. private static class PlanetArrayAdapter extends ArrayAdapter<Planet> {
  323.  
  324. private LayoutInflater inflater;
  325.  
  326. public PlanetArrayAdapter( Context context, List<Planet> planetList ) {
  327. super( context, R.layout.simplerow, R.id.rowTextView, planetList );
  328. // Cache the LayoutInflate to avoid asking for a new one each time.
  329. inflater = LayoutInflater.from(context) ;
  330. }
  331.  
  332. @Override
  333. public View getView(int position, View convertView, ViewGroup parent) {
  334. // Planet to display
  335. Planet planet = (Planet) this.getItem( position );
  336.  
  337. // The child views in each row.
  338. CheckBox checkBox ;
  339. TextView textView ;
  340.  
  341. // Create a new row view
  342. if ( convertView == null ) {
  343. convertView = inflater.inflate(R.layout.simplerow, null);
  344.  
  345. // Find the child views.
  346. textView = (TextView) convertView.findViewById( R.id.rowTextView );
  347. checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
  348.  
  349. // Optimization: Tag the row with it's child views, so we don't have to
  350. // call findViewById() later when we reuse the row.
  351. convertView.setTag( new PlanetViewHolder(textView,checkBox) );
  352.  
  353. // If CheckBox is toggled, update the planet it is tagged with.
  354. checkBox.setOnClickListener( new View.OnClickListener() {
  355. public void onClick(View v) {
  356. CheckBox cb = (CheckBox) v ;
  357. Planet planet = (Planet) cb.getTag();
  358. planet.setChecked( cb.isChecked() );
  359.  
  360. if (cb.isChecked()){
  361. FavList.add(planet.name);
  362. }
  363. else{
  364. FavList.remove(planet.name);
  365. }
  366. }
  367. });
  368. }
  369. // Reuse existing row view
  370. else {
  371. // Because we use a ViewHolder, we avoid having to call findViewById().
  372. PlanetViewHolder viewHolder = (PlanetViewHolder) convertView.getTag();
  373. checkBox = viewHolder.getCheckBox() ;
  374. textView = viewHolder.getTextView() ;
  375. }
  376.  
  377. // Tag the CheckBox with the Planet it is displaying, so that we can
  378. // access the planet in onClick() when the CheckBox is toggled.
  379. checkBox.setTag( planet );
  380.  
  381. // Display planet data
  382. checkBox.setChecked( planet.isChecked() );
  383. textView.setText( planet.getName() );
  384.  
  385. return convertView;
  386. }
  387.  
  388. }
  389.  
  390. public Object onRetainNonConfigurationInstance() {
  391. return planets ;
  392. }
  393.  
  394. public void comments (View view) {
  395. Intent myintent = new Intent (getApplicationContext(), DB.class);
  396. myintent.putExtra("name", FavList);
  397. startActivity(myintent);
  398. }
  399. }
  400.  
  401. public class DB extends ListActivity {
  402.  
  403. private final String SAMPLE_DB_NAME = "myFriendsDb";
  404. private final String SAMPLE_TABLE_NAME = "friend";
  405.  
  406. /** Called when the activity is first created. */
  407. @Override
  408. public void onCreate(Bundle savedInstanceState) {
  409. super.onCreate(savedInstanceState);
  410.  
  411. ArrayList<String> NumList = getIntent().getStringArrayListExtra("name");
  412. ArrayList<String> results = new ArrayList<String>();
  413. SQLiteDatabase sampleDB = null;
  414.  
  415. try {
  416. sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
  417.  
  418. sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
  419. SAMPLE_TABLE_NAME +
  420. " (FirstName VARCHAR);");
  421.  
  422. for(String str: NumList){
  423. sampleDB.execSQL("INSERT INTO " +
  424. SAMPLE_TABLE_NAME +
  425. " ( FirstName ) Values ('"+str+"');");
  426. }
  427.  
  428.  
  429. Cursor c = sampleDB.rawQuery("SELECT FirstName FROM " +
  430. SAMPLE_TABLE_NAME +
  431. " where FirstName > 10 LIMIT 5", null);
  432.  
  433. if (c != null ) {
  434. if (c.moveToFirst()) {
  435. do {
  436. String firstName = c.getString(c.getColumnIndex("FirstName"));
  437. results.add("" + firstName);
  438. }while (c.moveToNext());
  439. }
  440. }
  441.  
  442. this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
  443.  
  444. } catch (SQLiteException se ) {
  445. Log.e(getClass().getSimpleName(), "Could not create or Open the database");
  446. } finally {
  447. if (sampleDB != null)
  448. sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
  449. sampleDB.close();
  450. }
  451. }
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement