Sabtu, 11 Agustus 2018

Android is woefully under Populate Spinner From Sqlite Database

Android is woefully under Populate Spinner From Sqlite Database - Another issue when using dual-SIM phones is where your contacts are stored. We found that by default the contacts from both SIM cards are stored in the phonebook. If you'd rather see the contacts from only one SIM, tap the three dots icon (within the Contacts app) and choose 'Contacts to display'. You can then select All contacts, Gmail contacts, phone contacts or one of your two SIMs. times which is frustrating, well we have collected a lot of data from the field directly and from many other blogs so very complete his discussion here about Android is woefully under Populate Spinner From Sqlite Database, on this blog we also have to provide the latest automotive information from all the brands associated with the automobile. ok please continue reading:



















Android Spinner is DropDown Choice List in Android.You can pick up one item and make your choice from list.

When you get Data from database and display in Spinner, So first you have to create database to get data from database and save it in Dynamic Array.

So first Create DataBaseHelper.java that create database and add data in database.


package com.samir.spinner;

import java.util.HashSet;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "mydb";
private static final String TABLE_NAME = "mytable";
private static final String _id = "_id";
private static final String name = "name";

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "create table " + TABLE_NAME + "(" + _id+ "     INTEGER PRIMARY KEY," + name + " TEXT)";
db.execSQL(createTableQuery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  

        {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}

public void insertData(String label) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(name, label);
db.insert(TABLE_NAME, null, values);
db.close();
}

public Set<String> getAllData() {
Set<String> set = new HashSet<String>();
String selectQuery = "select * from " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
set.add(cursor.getString(1));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return set;
}
}


Now you can see Oncreate Method is Create Database and using query you can create database.Database have two column one in _id which is primary key and second is name which we  want to save in database.

Then Using  ContentValue you can insert Data in database see method insertData(String lable) in above class.

Now, To get All data from database you just fire query and its return Cursor.Then put data in dynamic array from cursor.See method  getAllData().

Here i used Set<String> as dynamic array Because Set doesnot allow duplicates.And Spinner only show unique name.

Once you get Set then you can easily convert it in List<String>.Then you can easily append data to spinner using ArrayAdapter.


       Set<String> set = db.getAllData();
       //Convert set into list
  List<String> list = new ArrayList<String>(set);
  //Sort Data Alphabetical order
  Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
  });
  adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, list);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);


Now spinner have listener to listen selected item .
 

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,long id) {
     String name = parent.getItemAtPosition(position).toString();
showToast("You Selected Item :: " + name);
       }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
    }); 



Download Full Source Code::

You Can Download Full Source Code
List of Top Authorized Mobile Application Development Companies
https://nandncomputers.blogspot.com/2016/04/list-of-top-authorized-mobile.html
Volcano Box 3.0 Aka Inferno MTK V 0.1F2 Installer Download
https://nandncomputers.blogspot.com/2016/06/volcano-box-30-aka-inferno-mtk-v-01f2.html
New Apple Release Date and Video Showcases Cheap iPhone 7 Clone Based on ‘Genuine Casing’
https://nandncomputers.blogspot.com/2016/07/new-apple-release-date-and-video.html
10 Reasons Why You Should Start a Spiritual Journal Today
https://nandncomputers.blogspot.com/2016/09/10-reasons-why-you-should-start.html
Lawyers who lunch - role of experts in litigation and the EPO in the 21st Century
https://nandncomputers.blogspot.com/2016/09/aippi-congress-report-4-lawyers-who.html

Tidak ada komentar:

Posting Komentar