如何刷新Android的列表视图多次? [英] How to refresh Android listview multiple times?

查看:113
本文介绍了如何刷新Android的列表视图多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我实现了对ArrayList中的删除操作。

      voiceUrl_list.remove(位置);
     

  2. 我用listvoice.invalidateViews(); //导致所有的意见进行重建,并重绘!

据刷新视图,一旦显示出来,但在进一步点击,应用程序被坠毁。

这里的code!

  convertView.setOnClickListener(新View.OnClickListener(){

                @覆盖
                公共无效的onClick(视图v){



                    voiceURL = voiceUrl_list.get(位置);


                    Log.i(_ voice_url_clicked_,+ voiceURL);
                    敬酒= NULL;
                    尝试
                    {
                        player.setDataSource(voiceURL); //设置的歌声从http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL到媒体播放器的数据源

                        播放器prepare()。 //你必须设置之后,调用此方法的setDataSource方法的数据源。调用prepare()后的MediaPlayer的实例,从网址到内部缓冲区开始加载数据。
                    }
                    赶上(例外五)
                    {
                        e.printStackTrace();
                    }
                    如果(!player.isPlaying())
                    {
                        player.start();
                        Toast.makeText(Call_In_Out.this,播放音乐......,5000).show();

                       Log.i(响应,UU+一);


                    }
                    其他
                    {
                        player.pause();

                        Log.e(暂停***,内部else块***********暂停);
                    }

                    player.setOnCompletionListener(新
                            OnCompletionListener(){

           @覆盖


 公共无效onCompletion(MediaPlayer的为arg0){

                                              Toast.makeText(getApplicationContext(),播放完毕,2000年).show();



                                              voiceURL = voiceUrl_list.get(位置);



//去掉数组列表条目

                                                voiceUrl_list.remove(位置);

                                                listvoice.invalidateViews(); //导致所有的意见进行重建,并重绘!


                                         }
                                      });
 

错误如图logcat的:

  7月4号至17号:42:42.898:E / AndroidRuntime(5676):致命异常:主要
七月四日至17日:42:42.898:E / AndroidRuntime(5676):显示java.lang.NullPointerException
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在com.zoemultiline.call.Call_In_Out $ ListVoiceAdapter $ 1.onClick(Call_In_Out.java:427)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.view.View.performClick(View.java:4204)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.view.View $ PerformClick.run(View.java:17355)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.os.Handler.handleCallback(Handler.java:725)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.os.Handler.dispatchMessage(Handler.java:92)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.os.Looper.loop(Looper.java:137)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在android.app.ActivityThread.main(ActivityThread.java:5041)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在java.lang.reflect.Method.invokeNative(本机方法)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在java.lang.reflect.Method.invoke(Method.java:511)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
七月四日至17日:42:42.898:E / AndroidRuntime(5676):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

刚刚更新数组或数据从列表视图中被填满,然后调用方法如下:

  YO​​UR_ADAPTER.notifyDataSetChanged();
 

希望它会帮助你。

  1. I've implemented the remove operation on arraylist as

    voiceUrl_list.remove(position);
    

  2. I've used listvoice.invalidateViews(); // causes all the views to be rebuilt and redrawn!

It refreshes the view, and displays it once, but on clicking further, app gets crashed.

Here's the code!

convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {



                    voiceURL=voiceUrl_list.get(position);


                    Log.i("_voice_url_clicked_"," "+voiceURL);  
                    toast = null;
                    try 
                    {
                        player.setDataSource(voiceURL); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source

                        player.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer.
                    } 
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                    }
                    if(!player.isPlaying())
                    {
                        player.start();
                        Toast.makeText(Call_In_Out.this,"Playing Audio ...",5000).show();

                       Log.i("response is"," uu"+a);


                    }
                    else 
                    {
                        player.pause();

                        Log.e("Pause ***","Inside Else Block***********PAUSE");
                    }   

                    player.setOnCompletionListener(new
                            OnCompletionListener() {

           @Override


 public void onCompletion(MediaPlayer arg0) {

                                              Toast.makeText(getApplicationContext(),"Finished Playing",2000).show();



                                              voiceURL=voiceUrl_list.get(position);



//removing the arraylist entries                                    

                                                voiceUrl_list.remove(position);

                                                listvoice.invalidateViews(); // causes all the views to be rebuilt and redrawn !


                                         }
                                      });

Error as shown in Logcat:

04-17 07:42:42.898: E/AndroidRuntime(5676): FATAL EXCEPTION: main
04-17 07:42:42.898: E/AndroidRuntime(5676): java.lang.NullPointerException
04-17 07:42:42.898: E/AndroidRuntime(5676):     at com.zoemultiline.call.Call_In_Out$ListVoiceAdapter$1.onClick(Call_In_Out.java:427)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.view.View.performClick(View.java:4204)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.view.View$PerformClick.run(View.java:17355)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.os.Handler.handleCallback(Handler.java:725)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.os.Looper.loop(Looper.java:137)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at java.lang.reflect.Method.invoke(Method.java:511)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-17 07:42:42.898: E/AndroidRuntime(5676):     at dalvik.system.NativeStart.main(Native Method)

解决方案

just update the array or data from which list view is filled up and then call the method below.

YOUR_ADAPTER.notifyDataSetChanged();

Hope it will help you.

这篇关于如何刷新Android的列表视图多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆