简体   繁体   中英

AsyncTask doInbackground returns arraylist value but in onPostExecute arraylist is 0

In Asynctask doInBackground I get arraylist from firebase database and return arraylist(return 30 maps) result but in onPostExecute arraylist is 0 (doesn't keep data that was in doInbackground). I think problem in - doInBackground work like a parallel thread. Ok. I also tried to insert data which is in doInbackground to onCreate but it's not working also(return arraylist 0). That's my code. What is the wrong with my code?

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        ParseTitle parseTitle = new ParseTitle();
        parseTitle.execute();
    }
public class ParseTitle extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        avLoadingIndicatorView = (com.wang.avi.AVLoadingIndicatorView) findViewById(R.id.avi);
        avLoadingIndicatorView.show();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
        arraylist = new ArrayList<HashMap<String, String>>();
        database = FirebaseDatabase.getInstance();
        ref1 = database.getReference("work");
        ref = ref1.child("worker");
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    String s_position = (String) ds.child("v_position").getValue();
                    String s_employer = (String) ds.child("v_company").getValue();
                    String s_posted = (String) ds.child("v_post").getValue();
                    String s_link = (String) ds.child("v_url").getValue();

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("POSITION", s_position);
                    map.put("EMPLOYER", s_employer);
                    map.put("POSTED", s_posted);
                    map.put("LINK", s_link);
                    map.put("TYPE", "IT");
                    arraylist.add(map);
                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
               // System.out.println("The read failed: " + DatabaseError.DATA_STALE);
            }
        });
        return  arraylist;
    }
    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
        listView = (ListView) findViewById(R.id.listview);
      //  Collections.sort(arraylist,SortByValue);
        //HashMap<String, Integer> sortedMap = (HashMap<String, Integer>) sortByValue(arraylist);
       // if(arraylist != null && arraylist.size() > 0) {
            adapter = new ListViewAdapter(IT.this, arraylist);
            listView.setAdapter(adapter);
            avLoadingIndicatorView.hide();
            findViewById(android.R.id.content).setBackgroundColor(Color.TRANSPARENT);
      //  }
    }
}

It will be very good if somebody help me with trouble appointed above. :)

replace:

adapter = new ListViewAdapter(IT.this, arraylist);

with

adapter = new ListViewAdapter(IT.this, result);

The whole AsyncTask is not needed because the FirebaseDatabase events are called asynchronous. Your doInBackground just runs through really quick and onPostExecute is called before you receive the result in onDataChange . Without the AsyncTask it could look like this:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        ParseTitle parseTitle = new ParseTitle();
        parseTitle.execute();
avLoadingIndicatorView = (com.wang.avi.AVLoadingIndicatorView) findViewById(R.id.avi);
avLoadingIndicatorView.show();
arraylist = new ArrayList<HashMap<String, String>>();
database = FirebaseDatabase.getInstance();
ref1 = database.getReference("vacancies");
ref = ref1.child("jobsearch");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()) {
            String s_position = (String) ds.child("v_position").getValue();
            String s_employer = (String) ds.child("v_company").getValue();
            String s_posted = (String) ds.child("v_post").getValue();
            String s_link = (String) ds.child("v_url").getValue();

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("POSITION", s_position);
            map.put("EMPLOYER", s_employer);
            map.put("POSTED", s_posted);
            map.put("LINK", s_link);
            map.put("TYPE", "IT");
            arraylist.add(map);
        }
        adapter = new ListViewAdapter(IT.this, arraylist);
        listView.setAdapter(adapter);
        avLoadingIndicatorView.hide();
        findViewById(android.R.id.content).setBackgroundColor(Color.TRANSPARENT);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM