using arraylist to populate the existing data in sqlite into spinner
Hi guys i have trouble to bind the existing data from sqlite to spinner. Do i have to apply arraylist ? If so how do i do it? Please help me i'm still new and this is for my project.
this is how the data taken from database :
public Cursor alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
return cursor;
}
this is the coding that supposedly to receive the data and show it on spinner but i have no idea to do it :
SpListofShop = (Spinner) findViewById(R.id.SpListofShop);
Cursor cursor = db.alldata();
if(cursor.getCount()==0)
{
Toast.makeText(getApplicationContext(), "NO DATA", Toast.LENGTH_SHORT).show();
}
else
{
while(cursor.moveToNext())
{
}
}
i really hope any of you can help me i'm too desperate
Coding to call the public arraylist function
this is the coding to call the public arraylist in databasehelper.java :
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, db.alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
add a comment |
Hi guys i have trouble to bind the existing data from sqlite to spinner. Do i have to apply arraylist ? If so how do i do it? Please help me i'm still new and this is for my project.
this is how the data taken from database :
public Cursor alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
return cursor;
}
this is the coding that supposedly to receive the data and show it on spinner but i have no idea to do it :
SpListofShop = (Spinner) findViewById(R.id.SpListofShop);
Cursor cursor = db.alldata();
if(cursor.getCount()==0)
{
Toast.makeText(getApplicationContext(), "NO DATA", Toast.LENGTH_SHORT).show();
}
else
{
while(cursor.moveToNext())
{
}
}
i really hope any of you can help me i'm too desperate
Coding to call the public arraylist function
this is the coding to call the public arraylist in databasehelper.java :
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, db.alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
you dont need any array or list andwhile(cursor.moveToNext())loop - just useSimpleCursorAdapterinstead
– pskink
Nov 24 '18 at 8:46
and no - do NOT use anyArrayAdapters for that
– pskink
Nov 24 '18 at 8:57
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
why? because you dont have to use any loops for popolate tne list/arrau used byArrayAdapter,- withSimpleCursorAdapteryou simply pass aCursorin the constructor - thats ALL - also as a bonus you have valididinOnItemSelectedListener#onItemSelectedmethod
– pskink
Nov 24 '18 at 9:25
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48
add a comment |
Hi guys i have trouble to bind the existing data from sqlite to spinner. Do i have to apply arraylist ? If so how do i do it? Please help me i'm still new and this is for my project.
this is how the data taken from database :
public Cursor alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
return cursor;
}
this is the coding that supposedly to receive the data and show it on spinner but i have no idea to do it :
SpListofShop = (Spinner) findViewById(R.id.SpListofShop);
Cursor cursor = db.alldata();
if(cursor.getCount()==0)
{
Toast.makeText(getApplicationContext(), "NO DATA", Toast.LENGTH_SHORT).show();
}
else
{
while(cursor.moveToNext())
{
}
}
i really hope any of you can help me i'm too desperate
Coding to call the public arraylist function
this is the coding to call the public arraylist in databasehelper.java :
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, db.alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
Hi guys i have trouble to bind the existing data from sqlite to spinner. Do i have to apply arraylist ? If so how do i do it? Please help me i'm still new and this is for my project.
this is how the data taken from database :
public Cursor alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
return cursor;
}
this is the coding that supposedly to receive the data and show it on spinner but i have no idea to do it :
SpListofShop = (Spinner) findViewById(R.id.SpListofShop);
Cursor cursor = db.alldata();
if(cursor.getCount()==0)
{
Toast.makeText(getApplicationContext(), "NO DATA", Toast.LENGTH_SHORT).show();
}
else
{
while(cursor.moveToNext())
{
}
}
i really hope any of you can help me i'm too desperate
Coding to call the public arraylist function
this is the coding to call the public arraylist in databasehelper.java :
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, db.alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
edited Nov 25 '18 at 7:20
j-h13
asked Nov 24 '18 at 8:38
j-h13j-h13
13
13
you dont need any array or list andwhile(cursor.moveToNext())loop - just useSimpleCursorAdapterinstead
– pskink
Nov 24 '18 at 8:46
and no - do NOT use anyArrayAdapters for that
– pskink
Nov 24 '18 at 8:57
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
why? because you dont have to use any loops for popolate tne list/arrau used byArrayAdapter,- withSimpleCursorAdapteryou simply pass aCursorin the constructor - thats ALL - also as a bonus you have valididinOnItemSelectedListener#onItemSelectedmethod
– pskink
Nov 24 '18 at 9:25
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48
add a comment |
you dont need any array or list andwhile(cursor.moveToNext())loop - just useSimpleCursorAdapterinstead
– pskink
Nov 24 '18 at 8:46
and no - do NOT use anyArrayAdapters for that
– pskink
Nov 24 '18 at 8:57
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
why? because you dont have to use any loops for popolate tne list/arrau used byArrayAdapter,- withSimpleCursorAdapteryou simply pass aCursorin the constructor - thats ALL - also as a bonus you have valididinOnItemSelectedListener#onItemSelectedmethod
– pskink
Nov 24 '18 at 9:25
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48
you dont need any array or list and
while(cursor.moveToNext()) loop - just use SimpleCursorAdapter instead– pskink
Nov 24 '18 at 8:46
you dont need any array or list and
while(cursor.moveToNext()) loop - just use SimpleCursorAdapter instead– pskink
Nov 24 '18 at 8:46
and no - do NOT use any
ArrayAdapters for that– pskink
Nov 24 '18 at 8:57
and no - do NOT use any
ArrayAdapters for that– pskink
Nov 24 '18 at 8:57
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
why? because you dont have to use any loops for popolate tne list/arrau used by
ArrayAdapter,- with SimpleCursorAdapter you simply pass a Cursor in the constructor - thats ALL - also as a bonus you have valid id in OnItemSelectedListener#onItemSelected method– pskink
Nov 24 '18 at 9:25
why? because you dont have to use any loops for popolate tne list/arrau used by
ArrayAdapter,- with SimpleCursorAdapter you simply pass a Cursor in the constructor - thats ALL - also as a bonus you have valid id in OnItemSelectedListener#onItemSelected method– pskink
Nov 24 '18 at 9:25
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48
add a comment |
1 Answer
1
active
oldest
votes
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT useArrayAdapterwhen your data comes from aCursor- why do you insist in using it?
– pskink
Nov 24 '18 at 9:29
@j-h13,select *...return all data from your table, but, you want to show a specific name on the spinner, say, value ofnamerow. If so, replacerow_namewithname
– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456542%2fusing-arraylist-to-populate-the-existing-data-in-sqlite-into-spinner%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT useArrayAdapterwhen your data comes from aCursor- why do you insist in using it?
– pskink
Nov 24 '18 at 9:29
@j-h13,select *...return all data from your table, but, you want to show a specific name on the spinner, say, value ofnamerow. If so, replacerow_namewithname
– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
|
show 7 more comments
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT useArrayAdapterwhen your data comes from aCursor- why do you insist in using it?
– pskink
Nov 24 '18 at 9:29
@j-h13,select *...return all data from your table, but, you want to show a specific name on the spinner, say, value ofnamerow. If so, replacerow_namewithname
– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
|
show 7 more comments
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
1.change alldata as follows:
public ArrayList<String> alldata()
{
ArrayList<String> values = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("Select * from shop", null);
for (int i=0;i<cursor.getCount();i++){
values.add(cursor.getString(cursor.getColumnIndex("row_name")));
//edit, change row_name with your row
}
cursor.close();
return values;
}
2.change spinner code like this:
Spinner SpListofShop = findViewById(R.id.SpListofShop);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, alldata());
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
SpListofShop.setAdapter(adapter);
answered Nov 24 '18 at 8:52
Touhidul IslamTouhidul Islam
1,3141415
1,3141415
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT useArrayAdapterwhen your data comes from aCursor- why do you insist in using it?
– pskink
Nov 24 '18 at 9:29
@j-h13,select *...return all data from your table, but, you want to show a specific name on the spinner, say, value ofnamerow. If so, replacerow_namewithname
– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
|
show 7 more comments
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT useArrayAdapterwhen your data comes from aCursor- why do you insist in using it?
– pskink
Nov 24 '18 at 9:29
@j-h13,select *...return all data from your table, but, you want to show a specific name on the spinner, say, value ofnamerow. If so, replacerow_namewithname
– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
i wanna ask whether the row_name is the first data or the column name? i get confused and also thank you so much for the steps and the coding :) @touhidul islam
– j-h13
Nov 24 '18 at 9:26
@j-h13 i just answered above why you should NOT use
ArrayAdapter when your data comes from a Cursor - why do you insist in using it?– pskink
Nov 24 '18 at 9:29
@j-h13 i just answered above why you should NOT use
ArrayAdapter when your data comes from a Cursor - why do you insist in using it?– pskink
Nov 24 '18 at 9:29
@j-h13,
select *... return all data from your table, but, you want to show a specific name on the spinner, say, value of name row. If so, replace row_name with name– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13,
select *... return all data from your table, but, you want to show a specific name on the spinner, say, value of name row. If so, replace row_name with name– Touhidul Islam
Nov 24 '18 at 9:37
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@j-h13 has this answer solved your problem? if so, please accept it
– Touhidul Islam
Nov 24 '18 at 15:52
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
@TouhidulIslam sorry sir its not working sorry for late reply
– j-h13
Nov 24 '18 at 18:58
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456542%2fusing-arraylist-to-populate-the-existing-data-in-sqlite-into-spinner%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you dont need any array or list and
while(cursor.moveToNext())loop - just useSimpleCursorAdapterinstead– pskink
Nov 24 '18 at 8:46
and no - do NOT use any
ArrayAdapters for that– pskink
Nov 24 '18 at 8:57
may i know why i cant use ArrayAdapter ? the existing data have a table named shop with only a column called shopname. So the data that i am about display on spinner is the one in shopname. is it bc i only have one column ? or? @pskink
– j-h13
Nov 24 '18 at 9:23
why? because you dont have to use any loops for popolate tne list/arrau used by
ArrayAdapter,- withSimpleCursorAdapteryou simply pass aCursorin the constructor - thats ALL - also as a bonus you have valididinOnItemSelectedListener#onItemSelectedmethod– pskink
Nov 24 '18 at 9:25
ok now i see thank you for the info :) @pskink
– j-h13
Nov 24 '18 at 9:48