gpt4 book ai didi

java - ArrayList 无法添加对象

转载 作者:行者123 更新时间:2023-12-01 17:20:23 24 4
gpt4 key购买 nike

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

}

我搜索了这个问题,但确实找不到,所以感谢您的关注!

最佳答案

看起来您在调用“execute”之后立即调用“work”。

但是“执行”发生在后台线程中,并且可能尚未运行。尝试创建 RetrieveVideos.onPostExecute 方法并从那里调用“work”。

关于java - ArrayList<CustomObject> 无法添加对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203937/

24 4 0