using arraylist to populate the existing data in sqlite into spinner












-2















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);









share|improve this question

























  • 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













  • 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













  • ok now i see thank you for the info :) @pskink

    – j-h13
    Nov 24 '18 at 9:48
















-2















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);









share|improve this question

























  • 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













  • 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













  • ok now i see thank you for the info :) @pskink

    – j-h13
    Nov 24 '18 at 9:48














-2












-2








-2








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);









share|improve this question
















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);






android sqlite arraylist android-sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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













  • 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













  • 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













  • 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,- 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

















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












1 Answer
1






active

oldest

votes


















0














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);





share|improve this answer
























  • 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, 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











  • @TouhidulIslam sorry sir its not working sorry for late reply

    – j-h13
    Nov 24 '18 at 18:58











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
});


}
});














draft saved

draft discarded


















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









0














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);





share|improve this answer
























  • 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, 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











  • @TouhidulIslam sorry sir its not working sorry for late reply

    – j-h13
    Nov 24 '18 at 18:58
















0














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);





share|improve this answer
























  • 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, 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











  • @TouhidulIslam sorry sir its not working sorry for late reply

    – j-h13
    Nov 24 '18 at 18:58














0












0








0







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);





share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










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 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 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











  • @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 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft