Android SQL query problem - java

I'm doing a standard SQL query in Android:
String selection = "SELECT phraseA, phraseB FROM TableXYZ";
Cursor c1;
c1 = myDbHelper.myDataBase.rawQuery(selection,null);
c1.moveToNext();
while(!c1.isLast()){
toplist.add("Phrase: "+c1.getString(0)+" "+c1.getString(1);
c1.moveToNext();
}
c1.close();
The table is very small and the number of returned stirngs is less than 40. Toplist is an ArrayList. It is put into a ListView.To the best of my knowledge this should show me 40ish strings. Now, what happens is the strings are shown but then another load of empty ListView fields come up. I have no idea how. I thought my iteration might be wrong, but I can't find fault with it.

Aren't you skipping the last result always? I think you need: while(!c1.isAfterLast()) to keep from skipping the last item.
Also this line has a syntax error:
toplist.add("Phrase: "+c1.getString(0)+" "+c1.getString(1);
you are missing a ")" character.
As for your question, you aren't showing enough code to really see what the problem is. Are you sure you aren't adding any empty values to the ArrayList at some other point in your code? All you are showing here is how you are populating an array list, not how you are populating a ListView. Show your ListView code.
It would also be fairly trivial to attach the debugger to see what your ArrayList contains after this step, to see if the error is in this part of your code or somewhere else. Or for that matter even printing the ArrayList size to LogCat would help in debugging this.

Related

Adding user input strings to an ArrayList and printing these in a TextView

so I'm just getting started in Java/Android Development and currently working on an assignment which I've gotten to work the way I want it to except for one part. So in this app you are supposed to be able to add "sandwiches" to an order, and then have your order presented below. I have the user adding sandwiches through a ListView with clickable options, as that's the idea, but then when presenting the order I want to store and present all the clicks, i.e. all the mackor that "have been ordered" in the TextView below the ListView, however I only manage to store and present one selection at a time, and when another one is clicked, the previous one is replaced. I've been trying to read up on this and I'm thinking a for or do-while loop should probably be used for this? However I did try a couple different versions of that without getting it to work, so here I am.
So tl;dr everything is working the way I want it to except for the selections just "replacing" eachother instead of adding up - how do I add a String/object to the ArrayList everytime a user selects an option in the ListView instead of having it just switching these? I was advised to use ArrayList to store these selections, but right now it's not of much use as it's just holding one at a time lol. I'm sure there are a lot of other things not done ideally in this program as well but yeah. Any advice on how to get this to work properly would be much appreciated! I figured my XML shoulnd't be necessary to include as that's not the issue but I gladly will if needed.

Is there a simple way to explain making an ArrayList of objects or is there a problem with Ready to Program?

So I am trying to create an array list of objects for a class project an I've looked around the internet for answers to this question but I don't really understand what is incorrect in my code. I am new to coding so it is probably something really simple (it is probably Ready to program seeing as it is very not normal to put it lightly)! So I am trying to create an ArrayList of the object subscribers and whenever I try to create a normal ArrayList with ArrayList sub = new ArrayList(); Ready to program is fine but when I do ArrayList<subscriber> arraylist = new ArrayList<subscriber>(); it highlights both the < saying ( expected instead of this token and on the ( it says Invalid ShiftExpression. I have no idea how to fix these errors, also this is the whole code that could be affected by this:
static boolean done = false;
ArrayList<subscriber> arraylist = new ArrayList<subscriber>();
On line 2 of the pasted code (static boolean done = false;), there is another error caused by the object array. Due to this error the semicolon is highlighted with the syntax error of [ expected instead of this token. I am extremely confused with this last one and would like to know if this problem is to do with my code or with Ready to Program (probably my code). And before people ask, no I am not using HSA and I'm trying to learn proper java (I've seen lots of people hating on the buggy mess that is HSA and I am trying to move away from it.
Edit: It turns out it is a bug with Ready to Program. Sorry for wasting your guys' time!
For this to work you would have to have another class called subscriber. You can't just make up a word and put it in the angle brackets of the ArrayList. For example
ArrayList<Integer> list = new ArrayList<>()
Integer is not a made up word, it is an internal class object that that is used by the arraylist.
As for your second error i suspect you are not doing this code inside a method, and intstead just doing it outside of the class.
Overall tho it is very hard to see what you are doing via your description and two lines of code you have posted.
This is just a bug in Ready to Program and not in Java. I tried the exact same code in Visual Studio Code and it worked perfectly fine.

Issue with array?

I have a method leftup which has an onClick listener from a button. The purpose of method leftup is to do the following:
Get an array from resources xml
(Which contains names of my other activities)
Turn array into an array list
Remove a string from that array
(The current class name)
Convert the list back into an array
Get a random value from that array
Execute method goToActivity with parameter of that random value
I have spent hours on this task so far, because the logic of the code seems fine. The problem is, when I run the code, I get the infamous error:
Unfortunately, "App_Name" has stopped
Here is my code for method onClick and goToActivity:
Here is my array in the resources:
By the way, string activity state is the name of the activity :
I am sure the exception is in the method leftup, as none of the Toasts get executed, and the error happens immediately as I click on the button.
Thank you so much for all your help, I really appreciate it.
A fellow programmer,
{Rich}
Stack trace:
https://gist.github.com/anonymous/dcd63f51289aaf1a1933
Here is the stack trace because I couldn't format it, this is the git hub page.
I figured it out! After three long hours of tweaking the code, I finally got it! The arrayList had to be a linkedList. Thats it! Because, arrayList's aren't as flexible, and so one cannot remove or add items to them. I hope this answer helps, and saves you three long hours!
java.lang.UnsupportedOperationException
java.lang.reflect.InvocationTargetException
are the exceptions that happen on this issue. Just make sure to use a more flexible container. :)
{Rich}

Android: Display special characters in ListView

At this moment I am creating a small Android app, as a hobby.
It is not something serious: it makes a query from an sqlite database, and displays the results in a listview element. If you click on an item in the list, it makes another query with the content of the element, and displays the new results. (e.g. You tap on a city, it displays the restaurants in the city. If you tap on the city, it displays the foods available)
Now my problem is that most of these texts contain some special characters (like Ľ - u013D) that are not displayed correctly. And since they are not displayed correctly, I am unable to make further queries with them also.
I have tried many ways, mainly what I saw on this forum, however I am too noob for that unfortunately. This is what I have:
//DataBaseHandler is just a custom class creating and executing SQL queries, nothing special. It extends to SQLiteOpenHelper
DataBaseHandler db=new DataBaseHandler(this);
//getStations returns a List<String> object with the required items, contains raw strings
ArrayList<String> StationList=(ArrayList<String>) db.getStations(i.getStringExtra("jaratszam"));
db.close();
lv=(ListView) findViewById(R.id.listStation);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, R.layout.tv, StationList);
lv.setAdapter(aa);
I have tried to modify the strings I got back with Html.fromHtml, also CharSequence[], but none helped (I guess i did not use them correctly). I have modified my database also, I have changed the special characters to html codes, like
&#00E1;
Could you please enlighten me what should I do exactly?
Thanks in advance.
You probably need to user the correct character set for your String, although UTF-8 (which is the default) should support most foreign and special characters. Here's how you create a new String with a custom charset from an old String:
String newString = new String(oldString.getBytes(), Charset.YOUR_CHARSET_HERE);
Now my problem is that most of these texts contain some special
characters (like Ľ - u013D) that are not displayed correctly.
Try Encoding your string before putting it in database and decode it before using it in your application.

How do you show list length in StringTemplate

I want to show the number of search results that are going to appear in the table, is there a way to show the number of elements or do you have to compute it, ie I want to show something like this:
Found $results.size$ result$if(rest(contacts))$s$endif$.
The "s" correctly shows when there is more than one search result, but the $results.size$ returns nothing. Not sure if it matters, but im using Java, so its a List thats being passed in.
Found the answer, there is a length() function, ie:
Found $length(results)$ result$if(rest(contacts))$s$endif$.

Categories

Resources