i have looked at various things to try and resolve this, however there is something i am missing with the onCreate or something.
i can move between portrait and landscape no problem and i can even save some of the data. my problem is in certain spinners.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("NS1", ns1.getSelectedItemPosition());
outState.putInt("NS2", ns2.getSelectedItemPosition());
outState.putInt("NS3", ns3.getSelectedItemPosition());
outState.putInt("NS4", ns4.getSelectedItemPosition());
outState.putInt("NS5", ns5.getSelectedItemPosition());
outState.putInt("NS6", ns6.getSelectedItemPosition());
outState.putInt("AS", announcespinner.getSelectedItemPosition());
outState.putInt("FS", feastselectorspinner.getSelectedItemPosition());
outState.putInt("CS", choirspinner.getSelectedItemPosition());
outState.putInt("SS", saintspinner.getSelectedItemPosition());
outState.putBoolean("AK", akonly.isChecked());
outState.putBoolean("PK", pkonly.isChecked());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
feastselectorspinner.setSelection(savedInstanceState.getInt("FS"));
pkonly.setChecked(savedInstanceState.getBoolean("PK"));
akonly.setChecked(savedInstanceState.getBoolean("AK"));
//savedInstanceWait = false;
//updateNewSongSpinners();
ns1.setSelection(savedInstanceState.getInt("NS1"));
ns2.setSelection(savedInstanceState.getInt("NS2"));
ns3.setSelection(savedInstanceState.getInt("NS3"));
ns4.setSelection(savedInstanceState.getInt("NS4"));
ns5.setSelection(savedInstanceState.getInt("NS5"));
ns6.setSelection(savedInstanceState.getInt("NS6"));
saintspinner.setSelection(savedInstanceState.getInt("SS"));
choirspinner.setSelection(savedInstanceState.getInt("CS"));
announcespinner.setSelection(savedInstanceState.getInt("AS"));
}
it updates everything except the nsx spinners. i believe it has somethign to do with the fact these are changeable.
if you check/uncheck the pkonly or akonly checkboxes or select a different selection in feastselectorspinner, then the nsx spinner adjust based on the database query which is performed below
private void updateNewSongSpinners(){
if(!savedInstanceWait){
db = new DatabaseHandler(Announcer.this);
List<NewSong> listnewsong = db.getAkOrPkOrFeastSongs(akonly.isChecked(), pkonly.isChecked(), feastselectorspinner.getSelectedItem().toString());
List<String> list = new ArrayList<String>();
String t;
list.add("Select New Song");
for(NewSong lns : listnewsong){
t = "";
if(lns.getSongNum() < 10){
t = "00";
}else if(lns.getSongNum() < 100){
t = "0";
}
t += Integer.toString(lns.getSongNum()) + " " + lns.getSongTitle();
list.add(t);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Announcer.this, R.layout.list_item, list);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
//reset all spinners
ns1.setAdapter(adapter);
ns2.setAdapter(adapter);
ns3.setAdapter(adapter);
ns4.setAdapter(adapter);
ns5.setAdapter(adapter);
ns6.setAdapter(adapter);
}
}
What am i missing that is preventing the nsx spinners from going back to their saved position. the NS1...NS6 values retrieved are the correct numbers. i cannot debug this because it wont debug on my phone and my emulator when i switch between portrait and landscape the emulator does not do anything.
the savedInstanceWait was used to see if there was a race condition. i could not find one.
i am at a loss. apparently the feastselectorspinner onClick() is being called after the onRestoreInstanceState(). which is causing the spinners to reset obviously. i do not know why the feastselectorspinner calls its function and none of the others do to reset their fields, but anyways. here is the fix which i would still like to know why it was calling the onclick() after the restore()
private void updateNewSongSpinners(){
String str1, str2, str3, str4, str5, str6;
str1 = ns1.getSelectedItem().toString();
str2 = ns2.getSelectedItem().toString();
str3 = ns3.getSelectedItem().toString();
str4 = ns4.getSelectedItem().toString();
str5 = ns5.getSelectedItem().toString();
str6 = ns6.getSelectedItem().toString();
db = new DatabaseHandler(Announcer.this);
List<NewSong> listnewsong = db.getAkOrPkOrFeastSongs(akonly.isChecked(), pkonly.isChecked(), feastselectorspinner.getSelectedItem().toString());
List<String> list = new ArrayList<String>();
String t;
list.add("Select New Song");
for(NewSong lns : listnewsong){
t = "";
if(lns.getSongNum() < 10){
t = "00";
}else if(lns.getSongNum() < 100){
t = "0";
}
t += Integer.toString(lns.getSongNum()) + " " + lns.getSongTitle();
list.add(t);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Announcer.this, R.layout.list_item, list);
adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
//reset all spinners
ns1.setAdapter(adapter);
ns2.setAdapter(adapter);
ns3.setAdapter(adapter);
ns4.setAdapter(adapter);
ns5.setAdapter(adapter);
ns6.setAdapter(adapter);
//if available keep previously chosen song
for(int i = 0; i < list.size(); i++){
if(list.get(i).equals(str1)) ns1.setSelection(i);
if(list.get(i).equals(str2)) ns2.setSelection(i);
if(list.get(i).equals(str3)) ns3.setSelection(i);
if(list.get(i).equals(str4)) ns4.setSelection(i);
if(list.get(i).equals(str5)) ns5.setSelection(i);
if(list.get(i).equals(str6)) ns6.setSelection(i);
}
}
Related
I am an android noobie. What I am trying to do is to make this String an ArrayList. This is done. When i Print it On (with tv.setText) , the result is what i need but in this if i have right below i cannot find the "1".
The result i want to have is to store the text between the noumbers inside another ArrayList but to go there i have to be able to read the strings from the ArrayList.
public class MainActivity extends AppCompatActivity {
String text = "1Hello12People22Paul22Jackie21Anna12Fofo2";
TextView tv;
List<String> chars = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
PrinThemNow();
}
public void PrinThemNow(){
chars = Arrays.asList(text.split(""));
tv.setText(toString().valueOf(chars));
for(int i=0;i<chars.size();i++){
if(toString().valueOf(chars.get(i)) == " 1"){
Toast.makeText(this,"I found One",Toast.LENGTH_LONG).show();
//This if is not working while the TV's text shows " 1"
}
}
}
}
First, just a tip, from string to char[] you can use
char[] chars = myString.toCharArray();
because it has no sense to save a char array as a string ArrayList
but now the problem. you have your string and you wanna print the text between the numbers.
It's not really clear what is your goal but lets try.
I will suppose you used the char[] because it's 10 times better and easier
case 1) you wanna print text betweens "1"s
//lets loop the chars
bool firstOneFound = false;
int firstOccurrence = -1;
int secondOccurrence = -1;
int i = 0;
for(char c : chars){
//is it equals to 1?
if(c.equals('1')){
//check if we are already after the first 1
if(firstOneFound){
//if yes, we found the final one
secondOccurrence = i;
break;
}
else{
//this is the first occurrence
firstOccurrence = i;
firstOneFound = true;
}
}
i++;
}
if(firstOccurrence != -1 && secondOccurrence != -1){
String myFinalString = myString.subString(firstOccurrence, secondOccurrence);
}
case 2) you wanna print all text except numbers (maybe with a space instead)
for(char c : chars){
//check if it's a number
if(c >= '0' && c <= '9'){
//replace the number with anything else
c = ' '; //if you wanna have it as a space
}
}
//print the final string
String myFinalString = new String(chars);
NOTE:
You can also use ArrayList of string, just replace ' with "
hope it helps
As i am newbie in android i want to show my saved spinner value at the time of view of saved form
how can i show database saved value at the time of view for spinner
here is my code
Java activity file
Spinner spnAECust;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ae_view_edit_sales);
spnAECust = (Spinner) findViewById(R.id.spnAECust);
/* Get Customer List and Add Select Default */
cust = con.getAllCustomers();// from database getting list
List<Customer> custList = new ArrayList<Customer>();
Customer c = new Customer();
c.setId(Constants.Common.ZERO);
c.setNm("Select Customer");
custList.add(c);
custList.addAll(cust);
// Create and fill an ArrayAdapter with a bunch of "Customer" objects
ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
custList.toArray(new Customer[custList.size()]));
// Tell the spinner about our adapter
spnAECust.setAdapter(custArrayAdapter);
sa = con.getAirSalesActivityDetails(Integer.parseInt(saId));// get details from Sqlite
Customer cust = new Customer();
cust.setId(sa.getCustomerId());
spnAECust.setSelection(custArrayAdapter.getPosition(cust));// to set value saved in db
}
at tried setSelection but it maches index value rather than id value so i get abstract value to b selected please show me correct way to implement ...Thnks in advance
Here i got answer by my own
/* Get Customer List and Add Select Default */
cust = con.getAllCustomers();
List<Customer> custList = new ArrayList<Customer>();
Customer cst = new Customer();
cst.setId(Constants.Common.ZERO);
cst.setNm(Constants.Common.CUSTOMER_HINT);
custList.add(cst);
custList.addAll(cust);
/* Get Commodity List and Add Select Default */
comm = con.getAllCommodities();
List<Commodity> commList = new ArrayList<Commodity>();
Commodity cm = new Commodity();
cm.setId(Constants.Common.ZERO);
cm.setNm(Constants.Common.COMMODITY_HINT);
commList.add(cm);
commList.addAll(comm);
// Create and fill an ArrayAdapter with a bunch of "Customer" objects
ArrayAdapter<Customer> custArrayAdapter = new ArrayAdapter<Customer>(this, android.R.layout.simple_spinner_item,
custList.toArray(new Customer[custList.size()]));
int custIndex = 0;
// to set selected item
for (int r = 0; r < custArrayAdapter.getCount(); r++) {
if (sa.getCustomerId() == custArrayAdapter.getItem(r).getId()) {
custIndex = r;
break;
}
}
// Tell the spinner about our adapter
spnAECust.setAdapter(custArrayAdapter);
spnAECust.setSelection(custIndex);
// Create and fill an ArrayAdapter with a bunch of "Commodities" objects
ArrayAdapter<Commodity> commArrayAdapter = new ArrayAdapter<Commodity>(this, android.R.layout.simple_spinner_item,
commList.toArray(new Commodity[commList.size()]));
int commIndex = 0;
// to set selected item
for (int r = 0; r < commArrayAdapter.getCount(); r++) {
if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
commIndex = r;
break;
}
}
// Tell the spinner about our adapter
spnAEComodity.setAdapter(commArrayAdapter);
spnAEComodity.setSelection(commIndex);
used for loop to get index for saved value
int commIndex = 0;
// to set selected item
for (int r = 0; r < commArrayAdapter.getCount(); r++) {
if (sa.getCommodityId() == commArrayAdapter.getItem(r).getId()) {
commIndex = r;
break;
}
}
and add index to
spnAEComodity.setSelection(commIndex);
Basically this is not a problem in itself, my code works so far.
What I have is a App, that lets a user log in and depending on his ID in the db, he gets displayed his saved notes. For this view I have this part of code:
title = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
MyDbHandler dbh = new MyDbHandler(this);
for(int i = 0; i < 999; i++) {
content = dbh.getNoteTitle(id, i); //getNoteTitle(int, int) returns String
if(content != null && content != "0")
title.add(content);
else
break;
}
list.setAdapter(title);
As I said, this works so far.
Thing is - I am very unhappy with the use of ' break; ' here, as I learned during education, this shouldn't be used.
Is there a smoother way to approach this issue?
Also ' content != "0" ' should be ' ! content.equals("0") ' normally, right? But that one doesn't work then... Why is this?
I am not sure what are you trying to do. First of all you should use "equal" method for Strings. The condition "content != "0" will always be true, because you are comparing 2 different objects. The condition "! content.equals("0")" should return true most of the time (when the value is not "0") and probably you should use the debugger to see exactly what is the value of content.
Second if you want to take all the notes from the database and show them to the user you should have first a method in the SQLiteOpenHelper similar to (it is not efficient to interrogate the database for each item, plus the separation of concerns):
public ArrayList<String> getNotesList (int userID){
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TABLE_NAME, new String[] {MyDbHandler.COLUMN_NOTE_TITLE}, MyDbHandler.userID + "=" + userID,null, null, null, null);
ArrayList<String> list = null;
String noteTitle;
if (c != null && c.moveToFirst())
{
list = new ArrayList<String>(c.getCount());
for (int i = 0; i < c.getCount(); i++)
{
noteTitle = c.getString(c.getColumnIndex(MyDbHandler.COLUMN_SESSION_PATH));
list.add(noteTitle);
c.moveToNext();
}
}
c.close();
db.close();
return list;
I think you should not save notes that you don't want to use (e.g. null or "0"), so instead of checking here, I would check in the addNote method.
For the list initialization you have:
MyDbHandler dbh = new MyDbHandler(this);
ArrayList listData = dbh.getNotesList(id)
if (listData != null && listData.length != 0){
title = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listData.setAdapter(title);
}
I didn't test the code, but I hope it helps you. Good luck!
I have the following code in WeekDays.java
ArrayAdapter<TaskInAWeek> adapter1 = (ArrayAdapter<TaskInAWeek>) getListAdapter() ;
TaskInAWeek taski;
ArrayList<Long> checkedItms = new ArrayList<Long>();
ArrayList<Long> uncheckedItms = new ArrayList<Long>();
for(int i=0;i<values.size();i++)
{
Log.i("value's size:" +values.size()," ");
if(EditStatusAdapter.tog==1)
{
Log.i("i::"+i," ");
taski = adapter1.getItem(i);
long id = taski.getId();
Log.i("nid:"+id," ");
checkedItms.add(id);
}
else
{
Log.i("i::"+i," ");
taski = adapter1.getItem(i);
long id = taski.getId();
Log.i("nid:"+id," ");
uncheckedItms.add(id);
}
}
Its custom Array Adapter is EditStatusAdapter.java. I get a NullPointerException at this pttaski = adapter1.getItem(i);.
I have used similar one in one of my activity but it did work fine. I guess its due to the custom array adapter. But I could not ignore it as well. Can anyone tel me how to clear the exception?
I'm displaying a List of Objects in a MultipleChoiceDialog. Another List contains all Objects who are already checked.
My Lists:
List<Participant> participants = datasourceParticipant.getAllParticipants();
List<Participant> participantsConference = datasourceParticipant.getAllParticipants(conference.getId());
In order to display them in the MultipleChoiceDialog, I build my List like this:
participantsNames = new ArrayList<String>();
for(int i = 0; i < this.participants.size(); i++) {
participantsNames.add(i, participants.get(i).getFirstname() + " " + participants.get(i).getLastname());
}
participantConferenceNames = new ArrayList<String>();
for(int i = 0; i < this.participantsConference.size(); i++) {
participantConferenceNames.add(i, participantsConference.get(i).getFirstname() + " " + participantsConference.get(i).getLastname());
}
Afterwards, I create the necessary String array ...
final CharSequence[] items = participantsNames.toArray(new CharSequence[participantsNames.size()]);
to display it in the MultipleChoiceDialog
builder.setMultiChoiceItems(items, null, null);
How do I add the checkedItems to the MultipleChoiceDialog. Or is there a much easier way to do it?
You have to pass in a boolean[] instead of null with the values that you want checked. The most straightforward way to accomplish this is to use a set:
Set<Participant> set = new HashSet();
set.addAll(datasourceParticipant.getAllParticipants(conference.getId()));
boolean[] checked = new boolean[participants.size()];
for (int i =0; i < participants.size(); i ++) {
checked[i] = set.contains(participants.get(i));
}
....
builder.setMultiChoiceItems(items, checked, null);
For that to work your Participant class must implement hashCode();