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;
}
}
}
我搜索了这个问题,但我真的找不到,所以谢谢你的关注!