0

i have a problem with an ArrayList variable which contains object of Videos class that i created. The ArrayList<Videos> that i created seems to always stays empty, even if i add to it an object. Here is my code where i declare and use this ArrayList :

public class Search extends Activity implements View.OnClickListener {
SearchView buttonSearch;
public static String URL_ALLVIDEOS = "http://" + Connexion.IP_ADRESS + "/protubes_android/getVideos.php";
public String[] videosArray;
public ArrayList<Videos> videosListe = new ArrayList<Videos>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_search);
    RetrieveVideos rv = new RetrieveVideos();
    rv.execute();
    work();

}

private void initialization() {
    buttonSearch = (SearchView) findViewById(R.id.svSearch);
    buttonSearch.setOnSearchClickListener(this);
}
public void work(){

    videosArray = new String[videosListe.size()];
    for (int i = 0; i < videosListe.size(); i++) {
        videosArray[i] = videosListe.get(i).getTitre();
    }
    ListView listView = (ListView) findViewById(R.id.lvVideos);
    initialization();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videosArray);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getBaseContext(), videosArray[i], Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Search.this, Streaming_test.class);
            startActivity(intent);
        }
    });
}
@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.svSearch:
            Toast.makeText(this, "Searching . . .", Toast.LENGTH_LONG).show();
            break;
    }
}
class RetrieveVideos extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... strings) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("aUsername", "hi"));
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(URL_ALLVIDEOS);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = client.execute(httpPost, responseHandler);
            JSONArray jsonArray = new JSONArray(response);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Videos video = new Videos(jsonObject.getInt("id"), jsonObject.getString("chemin"), jsonObject.getString("titre"), jsonObject.getString("description"), jsonObject.getString("categorie"));
                videosListe.add(video);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

}

I searched the problem, but i really can't find it, so thank you for your attention !

2 Answers 2

5

It looks like you're calling "work" right after calling "execute".

However "execute" happens in a background thread and probably has't run yet. Try creating a RetrieveVideos.onPostExecute method and calling "work" from there.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, it worked ! Thank's i'll accept your answer in 10 minutes !
0

Add a OnPostExecte method in your RetrieveVideos class and call the work in this method. This will make sure that the work method gets called immediately after the your execute method is called in the background of AsyncTask

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.