I want to write to sqlite database checked day of weeks. For example, I have 7 checkBoxes, and I need to write checked items to database in one field. And when I will get this field from db, I need parse it.
How to do this better? Thanks
store it in integer type and parse with the method:
public int serializeDays(ArrayList<Boolean> isDayActive)
{
int result = 0;
for(int i = 0; i < 7; ++i)
result &= ((isDayActive.get(i) ? 1 : 0) << i);
return result;
}
when you read from db deserialize with the code:
public ArrayList<Boolean> deserializeDays(Integer fromBase)
{
ArrayList<Boolean> result = new ArrayList<Boolean>();
for(int i = 0; i < 7; ++i)
result.add((fromBase >> i) & 1 == 1 ? true : false);
return result;
}
Related
I am storing the value of spinner like this:
mVaccineName=String.valueOf(spinner.getSelectedItem());
VaccinationModel vaccineModelObject = new VaccinationModel(profileId,mVaccineName,mVaccineDate,
mVaccinationNotes);
If I want to edit the spinner value later, how do I set the spinner according to stored data in sharedpref using model class object like (vaccineModelObject.getmVaccineName)?
The setSelection() function takes the entry's index as its argument, so you'd have to figure out the index value of your string in the adapter array. E.g.:
int idx = 0;
for(int i = 0; i < vaccineNames.length; i++) {
if(vaccineNames[i].equals(vaccineModelObject.getmVaccineName)) {
idx = i;
break;
}
}
spinner.setSelection(idx);
i have solved my problem like this:
String compareValue = profileModelObject.getmGender();
if (!compareValue.equals(null)) {
int spinnerPostion = adapter.getPosition(compareValue);
mGenderSpinner.setSelection(spinnerPostion);
spinnerPostion = 0;
}
I want to get random data from database(sqlite) without repeat.Can anyone help me ..
DBManager *databaseManager = (DBManager *)[[DBManager alloc] init];
NSArray *array=[databaseManager readQuestionFromDatabase];
que=[array objectAtIndex:0];
self.lblQuestion.text=que.question;
[self.btnOption1 setTitle:que.questionoption1 forState:UIControlStateNormal];
[self.btnOption2 setTitle:que.questionoption2 forState:UIControlStateNormal];
[self.btnOption3 setTitle:que.questionoption3 forState:UIControlStateNormal];
[self.btnOption4 setTitle:que.questionoption4 forState:UIControlStateNormal];
This is another answer which uses same random number generation without repetition but avoids multiple query execution.
String randomRowData = "";
selQuery = "SELECT MYFIELD FROM MYTABLE ";
ArrayList<String> list = new ArrayList<Integer>(size);
for(int i = 1; i <= size; i++) {
list.add(MYFIELD_VALUE); // All DB Data here
}
Random rand = new Random();
while(list.size() > 0) {
int index = rand.nextInt(list.size());
randomRowData = list.remove(index); // Will display the rows without repetition
/* USE THE ROW DATA APPROPRIATELY */
}
This approach using Random Number Generation via java and ROWNUM for SQLITE should help you. But you might have to execute queries multiple times.
int DBSize = getDBSize();
int randomRowNum = 0;
ArrayList<Integer> list = new ArrayList<Integer>(size);
for(int i = 1; i <= size; i++) {
list.add(i);
}
Random rand = new Random();
while(list.size() > 0) {
int index = rand.nextInt(list.size());
randomRowNum = list.remove(index);
selQuery = "SELECT * FROM MYTABLE WHERE ROWNUM = " + randomRowNum + " ORDER BY SOME_UNIQUE_SORT_ORDER";
// EXECUTE SELECT QUERY AND YOU WOULD GET RANDOM ROWS here.
}
private int getDBSize ()
{
int retVal = 5;
retVal = // Select count(1) from myTable;
return retVal; //(Assuming I have 5 records in DB)
}
Note: Make sure your sort order in ORDER BY is unique. Else results would not be as expected.
I am making an app in which there are list of questions and respective answers.
Questions are in one string array, while answers are in another string array.
I have implemented the following in a wish to shuffle the questions. (Of course the answers need to be linked to that question, else meaningless)
Code:
selected_Q = new String[totalnoofQ];
selected_A = new String[totalnoofQ];
int[] random_code = new int[totalnoofQ];
for (int i = 0; i < totalnoofQ; i++)
{
random_code[i] = i;
}
Collections.shuffle(Arrays.asList(random_code));
for (int j = 0; j < totalnoofQ; j++)
{
int k = random_code[j];
selected_Q [j] = databank_Q [k];
selected_A[j] = databank_A [k];
}
The code reports no fatal error, but the selected_Q is still in sequential order. Why?
Could you please show me how can I amend the codes? Thanks!!!
You shuffle a list created using random_code, but random_code is not modified.
You need to create a temporary list based on random_code. Shuffle this list and then use it to fill the selected_X arrays.
Something like this should work :
int[] random_code = new int[totalnoofQ];
for (int i = 0 ; i < totalnoofQ ; i++) {
random_code[i] = i;
}
List<Integer> random_code_list = new ArrayList<Integer>(); // Create an arraylist (arraylist is used here because it has indexes)
for (int idx = 0 ; idx < random_code.length ; idx++) {
random_code_list.add(random_code[idx]); // Fill it
}
Collections.shuffle(random_code_list); // Shuffle it
for (int j = 0 ; j < totalnoofQ ; j++) {
int k = random_code_list.get(j); // Get the value
selected_Q[j] = databank_Q[k];
selected_A[j] = databank_A[k];
}
My problem is I have around 1000+ records in an Android App
string field1;
string field2;
string field3;
string field4;
//...
I want to search in this set of records and get the best results on two fields (field1 and field2).
Currently I read each record and compare() (string compare) with the text i want to search so that takes a long time.
What is the best method to perform search?
Store each records in SQLite DB and do "select query where like"
Hash-Mapped
? any other suggestions?
Or may be create an Index of the records and do search.
If you want to search for not exact matches, I would try to make an ArrayList of MyAppRecord where
public class MyAppRecord {
private String record;
private int deviance;
}
and get for each record the deviance of the String you want to find with:
public static int getLevenshteinDistance (String s, String t) {
if (s == null || t == null) {
throw new IllegalArgumentException("Strings must not be null");
}
int n = s.length(); // length of s
int m = t.length(); // length of t
if (n == 0) {
return m;
} else if (m == 0) {
return n;
}
int p[] = new int[n+1]; //'previous' cost array, horizontally
int d[] = new int[n+1]; // cost array, horizontally
int _d[]; //placeholder to assist in swapping p and d
// indexes into strings s and t
int i; // iterates through s
int j; // iterates through t
char t_j; // jth character of t
int cost; // cost
for (i = 0; i<=n; i++) {
p[i] = i;
}
for (j = 1; j<=m; j++) {
t_j = t.charAt(j-1);
d[0] = j;
for (i=1; i<=n; i++) {
cost = s.charAt(i-1)==t_j ? 0 : 1;
// minimum of cell to the left+1, to the top+1, diagonally left and up +cost
d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
}
// copy current distance counts to 'previous row' distance counts
_d = p;
p = d;
d = _d;
}
// our last action in the above loop was to switch d and p, so p now
// actually has the most recent cost counts
return p[n];
}
}
save it to your MyAppRecord-object and finally sort your ArrayList by the deviance of its MyAppRecord-objects.
Note that this could take some time, depending on your set of records. And NOTE that there is no way of telling wether dogA or dogB is on a certain position in your list by searching for dog.
Read up on the Levensthein distance to get a feeling on how it works. You may get the idea of sorting out strings that are possibly to long/short to get a distance that is okay for a threshold you may have.
It is also possible to copy "good enough" results to a different ArrayList.
hi to all im reading this format from a file
BOOKNO= [1]
From= [ 2011-02-28 07:00:52]
To= [2011-03-17 07:01:02]
Link= [http://www.example.com]
SINCE= [5] days.
BOOKNO= [2]
From= [ 2011-03-01 17:55:15]
To= [2011-03-30 17:55:21]
Link= [http://www.something.com]
SINCE= [3] days.
and i need to insert what is between the brackets only into an sqlite database
and its not working .....!!
any suggestions on how to fix it or any other good ideas on how to read it and insert it to my database columns
NOTE: addressString has the information that needs to be inserted into the tabel and i have 5 columns BOOK,FROM,TO,LINK, and SINCE and when i read the file i get the information for all the books (more than 1 book)... thats why im using indexof() and for loop 'J' so i can insert 5 columns and loop to insert the next 5....
this is the code:
db.open();
long idx;
String lines[] = {addressString};
String fields[] = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
for (int j=0; j<5; j++){
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
fields[i] = lines[i].substring(be+1, e);
idx = db.insertTitle(fields[0],fields[1],fields[2],fields[3],fields[4]);
}
}
hi to all again i've been playing around with my code and when i try to show the output on the screen with this code i get only number 1 on the screen it does not read all the text that is in address string
if (addressString != "didn't read titels"){
String lines[] = {addressString};
String fields[] = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
int be = lines[i].indexOf('[');
int e = lines[i].indexOf(']');
fields[i] = lines[i].substring(be+1, e);
myLoutputText.setText(fields[i]);
}
db.insertTitle(...) should be outside the for loop (controlled by j).