简体   繁体   English

数组列表 <CustomObject> 无法添加对象

[英]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方法

传安 ArrayList<customobject> 到接受 ArrayList 作为参数的 function<object> 在 Java<div id="text_translate"><p> 我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。</p><p> 这是我的 function:</p><pre> public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) { this.m_ArrData = arrData; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }</pre><p> 当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,</p><pre> m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter( (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));</pre><p> 处理这种情况的最佳方法是什么,谢谢</p></div></object></customobject> - Pass An ArrayList<CustomObject> to a function that accepts as parameter an ArrayList<Object> in Java

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能将 CustomType ArrayList 添加到 Object ArrayLists 的 ArrayList 中? - Why I can't add CustomType ArrayList to ArrayList of Object ArrayLists? 传安 ArrayList<customobject> 到接受 ArrayList 作为参数的 function<object> 在 Java<div id="text_translate"><p> 我正在编写一个通用的 java-android function,它将接受ArrayList<Object>作为其参数之一,这样我就可以在我的整个应用程序中使用,而不管 ArrayList 元素的类型如何。</p><p> 这是我的 function:</p><pre> public GenericDisplayAdapter(Activity activity, ArrayList<Object> arrData) { this.m_ArrData = arrData; inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }</pre><p> 当我尝试将我的ArrayList<CustomObject>作为参数传递给此 function 时,出现错误“无法从ArrayList<CustomObject>转换为ArrayList<Object> ”,</p><pre> m_LVStructDamageHeader.setAdapter(new GenericDisplayAdapter( (Activity)this, (ArrayList<Object>)arrStructDamageHeaders));</pre><p> 处理这种情况的最佳方法是什么,谢谢</p></div></object></customobject> - Pass An ArrayList<CustomObject> to a function that accepts as parameter an ArrayList<Object> in Java 无法添加到ArrayList - Can't add to ArrayList 无法将ModuleInfo对象添加到ArrayList <? extends ModuleInfo> - Can't add a ModuleInfo object to ArrayList<? extends ModuleInfo> JAVA不能向一个arrayList添加多个对象 - JAVA can't add more than one object to an arrayList 对ArrayList进行排序<CustomObject> - Sorting ArrayList<CustomObject> 序列化数组列表<CustomObject> - Serializing an Arraylist<CustomObject> 将IndexOf与arrayList中的customObject一起使用 - Using IndexOf with a customObject in an arrayList 无法在数组列表中添加值? - Can't add values in an arraylist? 无法在方法中添加到arrayList - Can't add to arrayList in method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM