[英]ArrayList<CustomObject> can't add an object
i have a problem with an ArrayList variable which contains object of Videos
class that i created. 我有一个ArrayList变量存在问题,该变量包含我创建的
Videos
类的对象。 The ArrayList<Videos>
that i created seems to always stays empty, even if i add
to it an object. 即使我向其中
add
一个对象,我创建的ArrayList<Videos>
似乎始终保持为空。 Here is my code where i declare and use this ArrayList : 这是我声明并使用此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 ! 我搜索了问题,但确实找不到,因此感谢您的关注!
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.
尝试创建RetrieveVideos.onPostExecute方法并从那里调用“工作”。
Add a OnPostExecte
method in your RetrieveVideos class and call the work
in this method. 在您的RetrieveVideos类中添加
OnPostExecte
方法,然后使用此方法调用work
。 This will make sure that the work method gets called immediately after the your execute method is called in the background of AsyncTask 这将确保在AsyncTask的后台调用execute方法后立即调用work方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.