Cursor is looping but repeats only the first value - android

I created a loop that will get the data from my cursor, however I noticed that even though it is looping(with the correct count) it only shows the first value.
int vv = 0;
if ((CR3.moveToFirst()) || CR3.getCount() !=0){
while (CR3.isAfterLast() == false) {
vendoName[vv] = CR3.getString(0);
vendoEsch[vv] = CR3.getString(1);
vendoAsch[vv] = CR3.getString(2);
vendoTag[vv] = CR3.getString(3);
vv++;
CR3.moveToNext();
}}
and when I call all my data( I only need the first three records)
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
SearchResults2 sr2 = new SearchResults2();
for(int j = 0;j < 3;j++)
{
sr2.setName(vendoName[j]);
sr2.setEsch(vendoEsch[j]);
sr2.setAsch(vendoAsch[j]);
sr2.setTag(vendoTag[j]);
results2.add(sr2);
}
I am putting inside a listview, when I check, it is showing always the first data.
This is an example I used as a reference to my code(It's almost the same except that I used an array to put my data from)
http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx
Am I doing something wrong which is why it is only getting the first piece of data?

Is it not easier to do something like this (if you don't need more than 3 results):
ArrayList<SearchResults2> results2 = new ArrayList<SearchResults2>();
CR3.moveToFirst();
for (i = 0; i < 3; i++) {
SearchResults2 sr2 = new SearchResults2();
sr2.setName(CR3.getString(0));
sr2.setEsch(CR3.getString(1));
sr2.setAsch(CR3.getString(2));
sr2.setTag(CR3.getString(3));
results2.add(sr2);
CR3.moveToNext();
}
I think that maybe the cursor doesn't iterate properly through your results in your while-loop and that's why you become one and the same result for the three items

Related

getStringArrayList get only the last list without consider the name

I try to send several lists to other activity so I wrote the following code:
ArrayList<String> sections = new ArrayList<String>();
for(int i=1; i<=last; i++)
{
sections.clear();
for(j = 0; j < size; j++)
{
sections.add(someText);
}
ourIntent.putStringArrayListExtra("sections_"+i, sections);
}
As you can see for every loop cycle of i, the name I give to the sent list is different (sections_1, sections_2, ...).
The sections list is cleared in each loop cycle. And in debug mode I can see that in every loop cycle the sections have the right list.
The problem is in the next activity. When I take the list, with the following code:
sections1 = extras.getStringArrayList("sections_1");
sections2 = extras.getStringArrayList("sections_2");
sections1 and sections2 get the same list, which is the last list that was inserted in putStringArrayListExtra.
Anyone can explain this behavior?
I believe this is because it's storing a reference to your ArrayList, and not a copy of the current "state" of the list on each iteration of your loop.
For example, you first insert a reference to your ArrayList when it contains 1 item. Next, you insert a reference to your ArrayList when it contains 2 items. Both are just references, so when you actually transition to the next Activity, it copies the full ArrayList.
To fix this, you could actually make a local copy of the ArrayList each time you loop.
ArrayList<String> sections = new ArrayList<String>();
for(int i=1; i<=last; i++) {
sections.clear();
for(j = 0; j < size; j++){
sections.add(someText);
}
// Creating a new local copy of the current list.
ArrayList<String> newList = newArrayList<>(sections);
// Inserting the local copy instead.
ourIntent.putStringArrayListExtra("sections_"+i, newList);
}

how to show some newest item listview?

I created a listview that connects to a database. I want to show some of the newest items (maybe 10 newest items) that will add in a random time? How can I accomplish this? This what I have tried:
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
But this doesn't work.
Sorry but, this code block makes no sense.
int i = String.valueOf(mKondisiList));
for(i = 0; i<=5 ; i++ ){
mKondisiList = myDbHelper.getListKondisi();
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
Collections.reverse(mKondisiList);
}
To explain what you have did;
you are getting the list from your database
initializing the list adapter
setting adapter to list
then reverse your order of the list (but you are not notifying the adapter)
You are doing this in a loop exactly 5 times.
What you need to do is:
get your list from database
then reverse your order of the list
initialize the list adapter
set adapter to list
I should look like this:
mKondisiList = myDbHelper.getListKondisi();
Collections.reverse(mKondisiList);
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);
If you want to show only limited number of items, you can do something like below:
ArrayList<Object> fullList = new ArrayList<>();
fullList = myDbHelper.getListKondisi();
Collections.reverse(fullList);
//Add first 10 item to your mKondisiList
for (int i = 0; i < 10; i++) {
mKondisiList.add(fullList.get(i));
}
adapterKondisi = new ListKondisiAdapter(this, mKondisiList);
lvKondisi.setAdapter(adapterKondisi);

Avoid that value gets 'overwritten' in arraylist

I am trying to save some values in a arraylist but somehow they all get overwritten ending up with only 1 value in the arraylist (200).
final String[] titles = new String[urls.length];
for (int i = 0; i < titles.length; i++){
ArrayList<Integer> valuesList = new ArrayList<Integer>();
valuesList.add(page.getTopicCount()); // returns 4 values (50,100,150,200)
System.out.println("Element: " + valuesList.toString());
// returns only value 200
}
The code page.getTopicCount() returns the 4 values in 1 line (50 100 150 200) only the last one (200) gets added to the arraylist but i am trying to find a way to save them all 4 seperately.
What options do i have? (SharedPreferences, saving to file, do i have to build another loop)?
I already did some research and ended up on this page but i don`t know if this will work.
Ps: the 4 values are constantly changing, thus it is no option to add them as:
valuesList.add("50");
valuesList.add("100");
etc..
Edit:
getTopicCount is part of a Saxparser class, see below code snippet:
public void endElement(final String uri, final String localName, final String qName)
throws SAXException {
//
} else if (localName.equals("topiccount")) {
in_topiccount = true;
sb = new StringBuilder();
}
//
else if (localName.equals("topiccount")) {
in_topiccount = false;
forumPage.setTopicCount(Integer.parseInt(sb.toString())); //returns 50 100 150 200
sb = null;
}
//
}
Inside the for loop, you are initializing the valuesList ArrayList. So each iteration, you create a new ArrayList and add one element to it and then discard it. As a result, at the end you only have a reference to the last ArrayList you created, which can only have 200 in it.
Presumably what you want to do is something like:
ArrayList<Integer> valuesList = new ArrayList<Integer>();
for (int i = 0; i < titles.length; i++){
valuesList.add(page.getTopicCount()); // returns 4 values (50,100,150,200)
}
System.out.println("Element: " + valuesList.toString());
Edit: Essentially, create the ArrayList once, and then insert elements into it in the for loop.

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

Array Being Overwritten with Last Index in Loop

I'm working on code that takes two arrays with strings (the strings are just sentences) and allocates them to classes which are held in another array (The Sentence class array shown below in the code).
So here's my problem. When popList() is called, the for loop runs through twice and works fine, putting the first index of addStrings and addTranslation into the first class in the array. However, when the loop indexes up and runs temp.sentence = addStrings[1] again, it OVERRIDES the first class's .sentence also. Then when temp.translations = addTranslations[1] runs again it OVERRIDES the first class's .translation.
So by the end of the loop, all of the arrays are filled with the same thing: the last index of addStrings and addTranslation. Every time it loops it overwrites all the indices before it with the index it's supposed to be putting in.
Anyone know what the problem is here? Thanks!
public class Sentence {
public String sentence;
public String translation;
Sentence() {
sentence = " ";
translation = " ";
}
}
private void popStrings() {
addStrings[0] = "我是你的朋友。"; addTranslations[0] = "I am your friend.";
addStrings[1] = "你可以帮助我吗?"; addTranslations[1] = "Could you help me?";
addStrings[2] = "我不想吃啊!"; addTranslations[2] = "I don't want to eat!";
}
//Fill Sentence array with string and translation arrays
private void popList() {
int i = 0;
Sentence temp = new Sentence();
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
}
You need to create new Sentence() inside the loop:
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
Sentence temp = new Sentence();
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
Otherwise you set sentence and translation continuously in the same object.

Categories

Resources