1

Firstly, i have an arraylist (the data from firebase)which contains stock Strings.

Then i need to pass those stock strings to the asynctask . The asynctask is going to get the json from an url so i can get other stock information for constructing my listview.

The problem is that i dont know how to perform the asynctask one by one. I read several posts on the forum but it not suitable for my case.

Here is my Asynctask code:

public class SearchingAsyncTask extends AsyncTask<String, String, HashMap<String, String>> {
private static final String TAG = SearchingAsyncTask.class.getSimpleName();
Searching listener;
int i=0;

public void setListener(Searching listener){
    this.listener = listener;
}

@Override
protected HashMap<String, String> doInBackground(String... arg0){
    HashMap<String, String> hashResult = new HashMap<>();
    getMoney18DailyJson(hashResult, arg0[0]);
    getMoney18RealJson(hashResult, arg0[0]);

    return hashResult;
}

@Override
protected void onPostExecute(HashMap<String, String> result){
    listener.getInfo(result);

}

Here is my searching interface:

public interface Searching {
void getInfo(HashMap<String, String> info); }

The place that i want to call my Asynctask

public class LoadBookMarkList implements Searching{
public  static ArrayList<String> quote = new ArrayList<>();

public HashMap mark=new HashMap();
private FirebaseAuth auth;
public int i;

public void inti() {
    i=0;

    auth = FirebaseAuth.getInstance();

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(auth.getUid()).child("BookMark");
    ref.keepSynced(true);
    ref.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    getQuote((Map<String,Object>) dataSnapshot.getValue());

                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    //handle databaseError
                }
            });
}

public void getQuote(Map<String,Object> users)  {

    //iterate through each user, ignoring their UID
    for (Map.Entry<String, Object> entry : users.entrySet()){

        //Get user map
        Map singleUser = (Map) entry.getValue();
        //Get phone field and append to list
        quote.add(singleUser.get("quote").toString());
    }

    SearchingAsyncTask searchBookMark = new SearchingAsyncTask();
    searchBookMark.setListener(LoadBookMarkList.this);
    searchBookMark.execute("0"+quote.get(i)); 
    //I want to loop the execute() so i can input all arraylist element in to
    //the asynctask

}

@Override
public void  getInfo(HashMap<String, String> info){

        mark.put("Quote", quote.get(i));
        mark.put("ComName", info.get("name"));
        mark.put("Price", info.get("np"));
        mark.put("Increase","-0.5");
        mark.put("InPer","-0.5");
        Search.bookMarkData.add(mark); //for Listview 
}

}

Siddharth Patel
  • 205
  • 1
  • 13
Ho Wong
  • 11
  • 2
  • `Object result = searchBookMark.execute("0"+quote.get(i)).get();` but it will block your Main Thread – jake oliver Apr 04 '18 at 05:19
  • https://stackoverflow.com/a/10049087/9186864 explains a lot for you. – jake oliver Apr 04 '18 at 05:21
  • I try this before, but i have the problem that the asynctask always stay in running and never go to finishing stage.... – Ho Wong Apr 04 '18 at 06:03
  • `//I want to loop the execute() so i can input all arraylist element in to //the asynctask` ??? Unclear what you want. If you want a loop than make a loop. After that you can tell if it works or not and ask for comments. – greenapps Apr 04 '18 at 07:56

1 Answers1

0

The Firebase Database client, already runs all network operations in a background thread. This means that all operations take place without blocking your main thread. Putting it in an AsyncTask does not give any additional benefits.

If you want to search your database based on a keyword, follow these steps:

  1. Create a new query based on the new filter

    Query query = yourRef.orderByChild("yourField").equalTo(searchText);
    
  2. Attach a listener to this query.

  3. Create a new adapter with the results of this new query, or update the existing one.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193