2013-10-05 43 views
0

我有一個ArrayList變量,其中包含我創建的Videos類的對象。我創建的ArrayList<Videos>似乎始終保持空白,即使我add它對象。 這裏是我的代碼,我聲明和使用此ArrayList:ArrayList <CustomObject>不能添加對象

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

} 

我搜索了問題,但我真的不能找到它,所以謝謝您的關注!

回答

5

看起來你在調用「execute」後立即調用「work」。

但是「執行」發生在後臺線程中,可能還沒有運行。 嘗試創建RetrieveVideos.onPostExecute方法並從那裏調用「work」。

+0

完美,它的工作!謝謝,我會在10分鐘內接受你的答案! – R00t

0

在您的RetrieveVideos類中添加一個OnPostExecte方法,並在此方法中調用work。 這將確保您的執行方法在AsyncTask後臺被調用後立即調用工作方法