3
  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)
Peter O.
  • 32,158
  • 14
  • 82
  • 96

2 Answers2

1

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.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • ! i'm confused with this YOUR_ADAPTER label, isit the ListView object or say ListVoiceAdapter Class which extends the BaseClass. –  Apr 17 '13 at 06:33
  • 1
    yes right...the baseadapter object that is related to your listview. – Rushabh Patel Apr 17 '13 at 06:40
  • 1) ListView listvoice; 2)listvoice.setAdapter(new ListVoiceAdapter()); so sir ! you're saying **listvoice** is the baseadapter object. Right? how to call notifyDataSetChanged() i've gone thru this link,but still i'm stuck [link](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview) –  Apr 17 '13 at 06:52
  • 1
    you need to create ListVoiceAdapter() object and use that object.notifyDataSetChanged() to refresh the listview. – Rushabh Patel Apr 17 '13 at 07:22
  • adapterObject.notifyDataSetChanged() worked same as listvoice.invalidateViews() ! the app is getting crashed on second click ! –  Apr 17 '13 at 07:49
0

To refresh a listview, update the array (or other data structure) that underlies it. Then call notifyDataSetChanged on the adapter for the list.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127