mercredi 25 novembre 2015

populate spinner with sqlite data

Hi I have created following code to populate spinner with list of data in column 1 of table stored in SQLite database. When I ran the program nothing appears on the spinner. I have pasted the code below. If someone could help me with the issue that would be highly appreciated.

public class MainActivity extends ActionBarActivity implements   
AdapterView.OnItemSelectedListener {
DatabaseHelper myDb;
Spinner group;
Spinner volt1;
Spinner sf;
Spinner energy;
String group_text;
String sf_text;
String energy_text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
volt1 = (Spinner) findViewById(R.id.spinner_voltage);



List<String>names = myDb.getAllNames();
ArrayAdapter<String> adaptervolt = new ArrayAdapter<String>  
(this,R.layout.spinner_layout,R.id.txt,names);
adaptervolt.setDropDownViewResource 
(android.R.layout.simple_spinner_dropdown_item);
volt1.setAdapter(adaptervolt);


}




public class DatabaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME2 = "Spark Energy Table";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}




public List<String> getAllNames() {


List<String> names = new ArrayList<String>();


SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM"+ TABLE_NAME2;

db.beginTransaction();

try {

Cursor cursor_volt = db.rawQuery(selectQuery, null);


if (cursor_volt.moveToFirst())
{
do {


names.add(cursor_volt.getString(0));

} while (cursor_volt.moveToNext());

cursor_volt.close();
}

db.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}

finally
{

db.endTransaction();

db.close();
}





return names;

}
}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">


<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>

</LinearLayout>



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"    
android:layout_width="match_parent"
android:layout_height="match_parent"   
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"  
tools:context=".MainActivity"
android:id="@+id/id1">


<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_voltage      
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />

</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire