0

Videos我对包含我创建的类对象的 ArrayList 变量有疑问。我创建的ArrayList<Videos>那个似乎总是空的,即使我add给它一个对象。这是我声明和使用此 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;
    }
}

}

我搜索了这个问题,但我真的找不到,所以谢谢你的关注!

4

2 回答 2

5

看起来您在调用“执行”之后立即调用“工作”。

然而,“执行”发生在后台线程中,可能还没有运行。尝试创建一个 RetrieveVideos.onPostExecute 方法并从那里调用“work”。

于 2013-10-06T00:00:46.727 回答
0

OnPostExecte在您的 RetrieveVideos 类中添加一个方法并work在此方法中调用。这将确保在 AsyncTask 的后台调用您的 execute 方法后立即调用 work 方法

于 2014-07-04T08:02:52.560 回答