AsyncTask doInbackground返回arraylist值,但在onPostExecute中,arraylist为0

时间:2018-04-12 06:05:42

标签: java android arrays arraylist android-asynctask

在Asynctask doInBackground 我从firebase数据库获取arraylist并返回arraylist(返回30个map)结果,但在 onPostExecute arraylist为0(不保留所有数据) doInbackground)。我认为 - doInBackground 中的问题就像并行线程一样。好。    我还尝试将 doInbackground 中的数据插入到onCreate中,但它也不起作用(返回arraylist 0)。   那是我的代码。我的代码有什么问题?

  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);
      //  }
    }
}

如果有人帮我解决上面指定的麻烦,那将是非常好的。 :)

2 个答案:

答案 0 :(得分:1)

取代:

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

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

答案 1 :(得分:1)

不需要整个AsyncTask,因为FirebaseDatabase事件被称为异步。在doInBackground收到结果之前,onPostExecute只是非常快速地调用onDataChange。 如果没有AsyncTask,它可能如下所示:

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);

    }
}