ArrayList <customobject>无法添加对象</customobject>

时间:2013-10-05 23:57:25

标签: java android arraylist android-asynctask

我有一个ArrayList变量的问题,它包含我创建的Videos类的对象。我创建的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;
    }
}

}

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

2 个答案:

答案 0 :(得分:5)

看起来你在调用“执行”后正在调用“工作”。

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

答案 1 :(得分:0)

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