get data from sqlite into edittext based on condition provided - android

I am working on an Android project where I am struck with a issue. I need to fetch data from SQLite db based on condition provided.
In precise I mean to say I need to get the data from SQLite where I provide condition an display that returned value in EditText.
Like:
SELECT SPECIALCHARCTER FROM <TABLENAME> WHERE POSITION = "0";
I need a good link to study this process. Can anyone please help me in issue.
You can check out sample code of DBHelper.java class.
I shall be very happy to see reply from u friends and help me in solving this issue.
Thanks & Regards,
Raghav

Try select statement in the following way
SELECT * FROM Contact where position LIKE '%"+0+"%'"
this may helps you

Sorry but you code pasted is a mess as it is not good formatted and filled with code that is commented out. Hard to see anything.
What I saw was this code:
EditText result=(EditText)findViewById(R.id.input);
Cursor c = db.rawQuery("SELECT SPECIALCHARCTER FROM " + TABLE + " where POSITION = 0", null);
int A = c.getColumnIndex("POSITION");
String strRet;
strRet = c.getString(A);
results.setText("" + strRet);
c.moveToNext();
This will fail for multiple reasons:
Your cursor c is never moved to the first place before you access it. So it will always fail
Calling c.moveToNext() at the end of the method is senseless
Your variable results is never initialized
My advice: Remove every UI element from this class and start reading some links Pratik provided in his answer.

here is the good links to work with the sqlite with android with many controls
http://www.vogella.de/articles/AndroidListView/article.html
http://marakana.com/forums/android/examples/55.html
http://saigeethamn.blogspot.com/2009/10/android-developer-tutorial-part-12.html

Related

query where relation contains any object from array

Some time ago i tried to use a BaaS service (parse.com), now I want to make a query to fetch a result set where parse' table contained tag values as "tag1, tag2, tag2"
I found similar question, but it doesn't work fo me, in my case I get row where TAG column equals one of tags item, but not contained one of them
query.whereContainedIn("Tags", Arrays.asList("tag1", "tag2"));
If someone had same issue please help me resolve it or give a correct way to figure it out

Implementing OR Condition using Parse Android SDK?

I am new to Parse and i am working on a Project that uses Android PARSE Sdk so i was wondering on how can i make a where query with or condition using the android sdk.
I want to make a query like this [Psuedo code]
Select * from employ where employId is ("1","2","3")
I found this on parse documentation, i don't know if it helps or not.
Edit:
This is what i found on PARSE but its not working
String[] id= {"1","2","3"};
query.whereContainedIn("employId ", Arrays.asList(id));
It returns me an empty list but if i query them one by one i get result... Can anyone tell me whats wrong ?
You can use whereContainedIn to select specific rows. See this post you can get more ideas. https://www.parse.com/questions/different-arrays-and-wherecontainedin-for-android.
List<String> employId = new ArrayList<String>();
employId.add("1"); employId.add("2"); employId.add("2");
query.whereContainedIn("employId", employId);
If you are still not clear. check this https://www.parse.com/docs/android_guide#queries
I have found the solution and i must say its pretty lame of PARSE to not mention this anywhere in there documentation.
The problem was that the values that i was using inwhereContainedIn method were of type String but in reality they were pointers to another table's row.
I was trying to get the values using only there ids[as it is displayed on parse] but instead i had to pass the whole object in order to retrieve them. That was the reason on why it was returning empty list.
The thing is Even though it displays IDs [pointer to object in a table] we cant search using only ID's instead we have to use complete Parse objects if we want to search a table based on Specific Object.

Android - Do I need to sort a collection for min and max?

I came here (SO) a few days ago to research how to get the min and max from a collection in Android and found a solution to the effect of the following (sorry haven't got a link to the actual answer I used):
Max = (TextView)findViewById(R.id.Max);
Collections.sort(list);
Max.setText(String.format("%.2f", Collections.max(list)));
My question is do I actually need to sort the list before pulling the min/max value? I have tried running the code without sorting the list and it seems to work OK. I am just worried because the answer I used definitely sorted the list first so I assume there must be a reason, I just don't know what it is!
In addition #BobbyDigital's answer who corectly points out the th method iterates over the complete list, I would just like to mention that the result of using the max function might depend on the type of the list elements. If you see the doc , it says that
Returns the maximum element of the given collection, according to the natural ordering of its elements.
If you see Why does Collections.max() not return actual max value for a Collection of String? question, the person used a list of Strings. On extracting max using the abve number he did not get the max number as it was returning the value that's the largest lexicographically. So, just to mention his code:
ArrayList<String> dirNo = new ArrayList<String>();
dirNo.add("1");
dirNo.add("2");
dirNo.add("3");
dirNo.add("4");
dirNo.add("5");
dirNo.add("6");
dirNo.add("7");
dirNo.add("8");
dirNo.add("9");
dirNo.add("10");
dirNo.add("11");
System.out.println("max : " + Integer.parseInt(Collections.max(dirNo))
+ "");
The above code gave 9 as the answer. So be careful while using it. You mgiht want to convert everything to Integer etc based on your needs.
P.S: The example is from the question mentioned and the answer is inspired from this answer by NPE on same question.
No it doesn't have to be sorted. The method iterates over the entire collection.
See the Java docs for the method!

Search Suggestion results displayed as blank/no text

I have included a Search Dialog in my Activity which works fine. However adding Search Suggestions gives me a little problem: The search suggestion entries are "empty".
I can see my content provider gets called (query(..)) and I return a MatrixCursor with several rows. The suggestions list also shows with (clickable) entries -- but are all blank. Blank as if the string I returned for SUGGEST_COLUMN_TEXT_1 and SUGGEST_COLUMN_TEXT_2 where an empty string.
The columns I use in the MatrixCursor are:
String[] columnNames = {"_ID", "SUGGEST_COLUMN_TEXT_1", "SUGGEST_COLUMN_TEXT_2", "SUGGEST_COLUMN_INTENT_EXTRA_DATA"};
I did try with just the _ID and SUGGEST_COLUMN_TEXT_1 column but same result.
EDIT: And I tried returning a simple "test" string as SUGGEST_COLUMN_TEXT_1 instead of something from my data.
I'm note quite sure what code is relevant here, so please ask for whatever may be needed to figure this out.
I have no idea for where to look for this bug, and my Google-Fu has failed me.
Thanks
(I would like to have added an 'android-search-suggestion' tag, but I'm newguy so it seems I cant)
(Thank you, Jcwenger for teaching the new guy :-)
The solution, from my comment above:
Found it. Use SearchManager.SUGGEST_COLUMN_TEXT_1 instead of "SUGGEST_COLUMN_TEXT_1".. (Same for the rest).The String SearchManager.SUGGEST_COLUMN_TEXT_1 maps to "suggest_text_1": http://developer.android.com/reference/android/app/SearchManager.html#SUGGEST_COLUMN_TEXT_1

compiling text for textview, including data from SQLite table

A stupid question that Im sure is really really simple - ive been trying to fiddle to solve it myself, but its all very new to me so any wisdom would be REALLY appreciated!
With an android application (using eclipse) if I want to compile text for a text view using some data that is stored in a sqlite database (I have a working dbhelper and table I want to use), what do I do?
For example, I want a text box to say something like:
"hello"+#user_name+"its been"+#days+"since your last visit!"
where #user_name and #days are data in a table which I can currently retreive in a list (only 1 value in the list though).
How do i compile a string to be shown in the text view (and assign this string to the view)?
Help!
String textViewValue="hello "+nameList.get(0)+" its been "+daysList.get(0)+" since youy last visit";
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(textViewValue);
You said you have stored it in arryalist right?
I am not sure if you have one ArrayList or two for Name and Days. (it would be good if you paste some of your code).
If your ArrayList is arrayList then you can get the name atleast like
"hello"+arrayList.get(0); // will retrive first element
Or best way use cursor and fire a query which will store output of following in curser
select Name,Days from your_table where Name='xyz';
then you can use folloing code to retrive data form curser (I am assuming there is only one row in cursor here)
if(cursor.moveToFirst()){
String s = "hello"+cursor.getString(0)+"Its been"+cursor.getString(1)+"days since your last visit!!";
}
now set this s in your textbox.

Categories

Resources