I am calling a function in a Class which will update a List Object. I have successfully get the desired object and change the values in it. How do I update back the original list?
Here is my code:
void updateTask(
Guid id, String title, String start, String end, String timeleft) {
final taskToBeUpdated = _tasks.firstWhere((element) => element.id == id);
taskToBeUpdated.title = title;
taskToBeUpdated.start = start;
taskToBeUpdated.end = end;
taskToBeUpdated.timeLeft = timeleft;
}
I know I can use the forloop to achieve this
for (var i = 0; i < _tasks.length; i++) {
if (_tasks[i].id == id) {
_tasks[i].title = title;
_tasks[i].start = start;
_tasks[i].end = end;
_tasks[i].timeLeft = timeleft;
}
}
But is there a shorter way?
Your first code should directly modify the object in the list since dart passes it by reference.
Try run this code to understand how this works:
List a = [{"dod":1}, {"dod":3}];
Map _b = a.firstWhere((e)=>e["dod"]==1);
_b["dod"] = 2;
print (a);
Related
hi
why load text from String array and set text to textview is very slow in big string array?
please help to me.
//get khotbe text from database and copy to khotbe activity
private void setkhotbetextarabicfarsi() {
this.sqliteDB = SQLiteDatabase.openOrCreateDatabase(this.getDatabasePath("aliname").getPath(), (SQLiteDatabase.CursorFactory) null);
Itemid = this.getIntent().getIntExtra("selectedFromListid", 1);
Cursor cursorLines = this.sqliteDB.rawQuery("SELECT * FROM khotbe where IDFehrest=" + this.Itemid , (String[]) null);
allrecs = cursorLines.getCount();
matn = new String[allrecs];
if (this.allrecs != 0) {
cursorLines.moveToFirst();
for (int i = 0; i < this.allrecs; ++i) {
String TextArabicOfKhotbe = cursorLines.getString(cursorLines.getColumnIndex("TextArabicOfKhotbe"));
int IDkhotbe = cursorLines.getInt(cursorLines.getColumnIndex("IDkhotbe"));
this.matn[i] = TextArabicOfKhotbe;
cursorLines.moveToNext();
}
}
and main code:
for(int var1 = 0; var1 < this.allrecs; ++var1) {
tvArabic = new JustifiedTextView(this);
tvArabic.setText(matn[var1]);
you are creating the textviews in loop that might making it slow.. try populating the array values using an adapter..
Also check the number of rows you are accessing from the DB. if they are huge in number, they would require more time to be fetched.
Use limit in that case.
I am trying to remove special character from arraylist.
Not getting click how to do this?
I have 3 editfield and filling text after certain conditions
means when 1 is filled then another can be filled. now when i click to save this. this returns an array like [hello,abc,zbz] for fields
private List<String> hashtagData;
hashtagData = new ArrayList<String>();
String status_message = status.getText().toString();
String status_message2 = status2.getText().toString();
String status_message3 = status3.getText().toString();
hashtagData.add(status_message);
hashtagData.add(status_message2);
hashtagData.add(status_message3);
But I am trying to remove "[]".
Thank you if anybody can help.
Here try this:
ArrayList<String> strCol = new ArrayList<String>();
strCol.add("[a,b,c,d,e]");
strCol.add(".a.a.b");
strCol.add("1,2,].3]");
for (String string : strCol) {
System.out.println(removeCharacter(string));
}
private String removeCharacter(String word) {
String[] specialCharacters = { "[", "}" ,"]",",","."};
StringBuilder sb = new StringBuilder(word);
for (int i = 0;i < sb.toString().length() - 1;i++){
for (String specialChar : specialCharacters) {
if (sb.toString().contains(specialChar)) {
int index = sb.indexOf(specialChar);
sb.deleteCharAt(index);
}
}
}
return sb.toString();
}
Create regex which matches with your criteria, then loop through your list.
String myRegex = "[^a-zA-Z0-9]";
int index = 0;
for (String your_string : list)
list.set(index++, s.replaceAll(myRegex, ""));
can use below function to remove special character from string using regular expressions.
using System.Text;
using System.Text.RegularExpressions;
public string RemoveSpecialCharacters(string str)
{
return Regex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
}
I need some advice how to implement this situation in my application.
I have array of bitmpaps, which I'm using to store different states of my Canvas, so I can use them in the future. Here is the code which I'm using :
private Bitmap[] temp;
// on user click happens this ->
if(index<5){
temp[index] = Bitmap.createBitmap(mBitmap);
index++;
}
So basically I want to save only the last 5 bitmaps depending on user's actions. The things which I want to learn is how can I update my array so I can always have the last 5 bitmaps.
Here is what I mean :
Bitmaps [1,2,3,4,5] -> after user clicks I want to delete the first bitmap, re-order the array and save the new one as the last..so my array should look like this : Bitmaps[2,3,4,5,6];
Any suggestions / advices which is the best way to do that?
Thanks in advance!
I just wrote this...
Use this code to initialise:
Cacher cach = new Cacher(5);
//when you want to add a bitmap
cach.add(yourBitmap);
//get the i'th bitmap using
cach.get(yourIndex);
Remember you can re implement the function get to return the ith "old" Bitmap
public class Cacher {
public Cacher(int max) {
this.max = max;
temp = new Bitmap[max];
time = new long[max];
for(int i=0;i<max;i++)
time[i] = -1;
}
private Bitmap[] temp;
private long[] time;
private int max = 5;
public void add(Bitmap mBitmap) {
int index = getIndexForNew();
temp[index] = Bitmap.createBitmap(mBitmap);
}
public Bitmap get(int i) {
if(time[i] == -1)
return null;
else
return temp[i];
}
private int getIndexForNew() {
int minimum = 0;
long value = time[minimum];
for(int i=0;i<max;i++) {
if(time[i]==-1)
return i;
else {
if(time[i]<value) {
minimum = i;
value = time[minimum];
}
}
return minimum;
}
}
I am getting an unusual result when attempting to place a value in an array.
I have an array table[] of a simple class result{ int score, long time, string ID}
Intention is to have a sort of leader board.
My code happily finds the correct place to insert a new score if it is in the top 10.
int ix = 0;
int jx = 10; //
while ( ix < jx )
{
if (points > sTable[ix].points)
{
// score is higher move records down
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx] = sTable[jx -1];
}
//now add new score
sTable[ix].score = score; // all good until here
sTable[ix].time = time;
}
ix++;
}
Problem is that when I try to insert the score using sTable[ix].score = score;
The value gets written to sTable[ix].score and also sTable[ix +1].score.
It is repeatable, it occurs at any value of ix, I have single stepped through the code and as far as I can tell the command only executes once.
Has anyone seen this before?
That because you copied the object reference to the next element in the array. You should copy the values, or create a new object:
Option A:
// score is higher move records down
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx].time = sTable[jx -1].time;
sTable[jx].score = sTable[jx -1].score;
}
//now add new score
sTable[ix].score = score; // all good until here
sTable[ix].time = time;
Option B:
for (jx = mNumRecords - 1; jx >ix ; jx--)
{
sTable[jx] = sTable[jx -1];
}
sTable[ix] = new Result(score, time, ""); // Or however you construct the object
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.