Why doesn't the test work if?



  • I have a simple question, but I'm not doing that. The Annex works with the OBD and the opening activity keeps an infu from OBD to arrayarray takes out the infu. listview♪ So every time you open it, activity Method onCreate and naturally, each time the same infa is opened array and naturally duplicated listview♪ I wrote a simple Flag check, it's kind of called, but it doesn't work and every time a list of the same ones. item Total listview It's getting bigger. ♪ ♪

    This is code :

    public class ListOfMyWords extends AppCompatActivity {
    

    private ProgressDialogClass dialog;
    private MyAdapterForWords adapterForWords;

    private boolean wasUpdate; //<=====

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_of_my_words);

    ListView lv = (ListView) findViewById(R.id.lvOfMyWords);
    adapterForWords = new MyAdapterForWords(getApplicationContext());
    lv.setAdapter(adapterForWords);
    lv.setOnItemClickListener(new EditAndDelete());
    
    dialog = new ProgressDialogClass(this);
    
    
     if (!wasUpdate) { //&lt;=====
        saveFromParse();
    }
    

    }

    private void saveFromParse() {

    dialog.showProgressDialog();
    
    new Thread(new Runnable() {
        @Override
        public void run() {
            ParseQuery&lt;ParseObject&gt; pq = ParseQuery.getQuery("MyDictionary");
            pq.findInBackground(new FindCallback&lt;ParseObject&gt;() {
                @Override
                public void done(List&lt;ParseObject&gt; list, ParseException e) {
                    if (e == null) {
                        for (int index = 0; index &lt; list.size(); index++) {
                            Word word = new Word(
                                    list.get(index).getString("hebrewWord"),
                                    list.get(index).getString("wordTranslation"),
                                    list.get(index).getString("wordTranslate"),
                                    list.get(index).getString("objectID"));
                            SingletonForWords.getSingletonForWords().getArrayWords().add(word);
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "sorry",
                                Toast.LENGTH_SHORT).show();
                    }
    
    
    
                    wasUpdate = true; //&lt;=====
    
    
    
                    dialog.hideProgressDialog();
                    adapterForWords.notifyDataSetChanged();
                }
            });
        }
    }).start();
    

    }



  • First, there's no need to create a new flow here, because the name of the method. findInBackground Says that the work is already done in a separate flow and the result comes in callback done

    Second, when an activist is created, a new copy of the class is created, and therefore all fields of the class are assigned https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html if there is no explicit initialization of fields. For type boolean default value false♪ That's why the data is downloaded at the start. From here on, your flag verification looks absolutely useless. This situation may result in the retention of data, for example, in bds, and whether or not data are available at the start, and if they are not downloaded.

    Thirdly, let's do the methods, classes, variable names that can answer the question, "What does this method/class/...?" This could remove some of the comments, since the name would be clear for what it was. Your method saveFromParse presupposes some kind of retention, even though he's actually uploading something.

    And fourthly, never make it clear. ThreadUse it at least. AsyncTaskbut it's better to replace it with something better. Otherwise, it would lead to leaks, inconceivable mistakes, incorrect work, etc.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2