I don't understand how to use SugarORM to save information from an array list.
for (int i =0 ; i<price.size(); i++){
List <OrdersSugarORM> ordersGo = new ArrayList<>();
ordersGo.add(new OrdersSugarORM(article.get(i)));
OrdersSugarORM.saveInTx(ordersGo);
}
I don't understand why you think it is a good idea to save an arraylist of a single element repeatedly using the for loop.
Try this instead - build the list, then save it. (Using the correct object types, since you have not provided what OrdersSugarORM is...)
List <OrdersSugarORM> ordersGo = new ArrayList<>();
for (int i = 0; i < price.size(); i++){
ordersGo.add(new OrdersSugarORM(article.get(i)));
}
OrdersSugarORM.saveInTx(ordersGo);
And maybe you want price.get(i), otherwise make sure that price.size() == article.size()
Related
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);
}
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);
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
I have an array adapter(string), and would like to convert it to a List<String>, but after a little googling and a few attempts, I am no closer to figuring out how to do this.
I have tried the following;
for(int i = 0; i < adapter./*what?*/; i++){
//get each item and add it to the list
}
but this doesn't work because there appears to be no adapter.length or adapter.size() method or variable.
I then tried this type of for loop
for (String s: adapter){
//add s to the list
}
but adapter can't be used in a foreach loop.
Then I did some googling for a method (in Arrays) that converts from an adapter to a list, but found nothing.
What is the best way to do this? Is it even possible?
for(int i = 0; i < adapter.getCount(); i++){
String str = (String)adapter.getItem(i);
}
Try this
// Note to the clown who attempted to edit this code.
// this is an input parameter to this code.
ArrayAdapter blammo;
List<String> kapow = new LinkedList<String>(); // ArrayList if you prefer.
for (int index = 0; index < blammo.getCount(); ++index)
{
String value = (String)blammo.getItem(index);
// Option 2: String value = (blammo.getItem(index)).toString();
kapow.add(value);
}
// kapow is a List<String> that contains each element in the blammo ArrayAdapter.
Use option 2 if the elements of the ArrayAdapter are not Strings.
String[] temp = new String[adapter.getCount()];
for(int i = 0; i < adapter.getCount(); i++)
temp[i] = adapter.getItem(i).toString();
List<String> list = Arrays.asList(temp);
Collections.sort(list);
adapter.clear();
comment = new Comment();
for(int i = 0; i < temp.length; i++)
{
comment.setComment(temp[i]);
System.out.println("comment is: " + comment.getComment());
adapter.insert(comment, i);
System.out.println("adapter is: " + adapter.getItem(i));
}
for(int i = 0; i < adapter.getCount(); i++)
System.out.println(adapter.getItem(i));
The code above performs the sorting of the ArrayAdapter which is typed ; a helper class as I am using SQLiteHelper and SQL database.
Ok so I verify, after clearing all data within the ArrayAdapter, that the data is added within a lexicographic sorted order.
However, by the time I reach the final for loop to verify this, the ArrayAdapter has replicated the last item in the list at every index. This is weird and makes no sense to me. Of course this is also reflected on the screen.
Can you provide assistance to understand what is going please?
You are using the same instance of 'Comment' throughout the ArrayAdapter. Hence, all positions of the ArrayAdapter have the exact same 'comment' object reference. This single instance has been set to the final string from the original list and so all ListView items will look the same. The solution is to move the instantiation of 'comment' into the for loop to create a unique 'comment' instance for each adapter position. I've also slightly optimized your code.
// -- Count used repeatedly, particularly in for loop - execute once here.
int orgCount = adapter.getCount();
String[] temp = new String[orgCount];
for(int i = 0; i < orgCount; i++)
temp[i] = adapter.getItem(i).toString();
List<String> list = Arrays.asList(temp);
Collections.sort(list);
// -- Prevent ListView refresh until all modifications are completed.
adapter.setNotifyOnChange(false);
adapter.clear();
for(int i = 0; i < temp.length; i++)
{
// -- Instantiation moved here - every adapter position needs a unique instance.
comment = new Comment();
comment.setComment(temp[i]);
System.out.println("comment is: " + comment.getComment());
// -- Changed from insert to add.
adapter.add(comment);
System.out.println("adapter is: " + adapter.getItem(i));
}
for(int i = 0; i < adapter.getCount(); i++)
System.out.println(adapter.getItem(i));
// -- Auto notification is disabled - must be done manually.
adapter.notifyDataSetChanged(true);
// -- All modifications completed - change notfication setting if desired.
adapter.setNotifyOnChange(true);
EDIT
Also, since you're inserting/adding one at a time, you might want to delay notifyDataSetChanged from executing until after all modifications are completed. This will prevent the ListView from refreshing on every modification. I've included it in the code above.
call adapter.notifyDataSetChanged() when all modification are done..