-1

I have been spending couple hours to try to update the textview inside the dialog, but failed.

When the option is clicked, there are new dialog is shown, and inside the dialog, there are textviews and button, when I click the button, the textview will be update.

Here is the code related to the button onClick listener:

 start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                for (int i = 0; i < 50 ; i ++){

                    final String currentNum = String.valueOf(i + 1);

                    Thread t = new Thread() {
                          @Override
                          public void run() {
                            runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                                  System.out.println(currentNum); //it is fine
                                  currentNum.setText(currentNum); //it is the problem, the setText only work when the for loop is finished.
                              }
                            });
                          }
                        };

                        t.start();

                }
            }
        });

Please let me know if you need more information. Thanks a lot in advance!

//it is a optionmenu

    case R.id.action_refresh:
    final TextView currentNum;
    final ImageButton start;

    String currentNum = Integer.toString(songList.size());

    final Dialog lyricsAnalysis = new Dialog(this,R.style.cust_dialog);
    lyricsAnalysis.requestWindowFeature(Window.FEATURE_NO_TITLE);
    lyricsAnalysis.setContentView(R.layout.analysis);
    lyricsAnalysis.setCancelable(true); //back button to cancel
    lyricsAnalysis.setCanceledOnTouchOutside(true); 


    start = (ImageButton) lyricsAnalysis.findViewById(R.id.start);

    //first value
    currentNum.setText(String.valueOf(currentNum));

    start.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            for (int i = 0; i < 50 ; i ++){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                updateTextView(lyricsAnalysis,i);

            }
        }
    });    
    lyricsAnalysis.show();
    lyricsAnalysis.getWindow().setLayout(600, 1000);
  break;
}
return super.onOptionsItemSelected(item); }
public void updateTextView(Dialog dialog, int i) {
    final TextView currentNum = (TextView) dialog.findViewById(R.id.currentNum);
    currentNum.setText(Stri`enter code here`ng.valueOf(i));
    //return;
}
andy
  • 5
  • 5
  • Can you please tell me the purpose of having thread within the onClick method? – Srinivasan Mar 31 '15 at 05:25
  • what's the use of thread here avoid that. – Karthika PB Mar 31 '15 at 05:26
  • As I though if not having the new thread, the textView will not be updated every time. And will just update it when the for loop is finished. And all I tried is that, the System.out.println(currentNum) is fine, it print out 0 to 49, however, the setText will only work when the for loop is finised and change the textView to (50) directly... – andy Mar 31 '15 at 06:50

2 Answers2

0

Try this method. This may helps you. It's work for me.(But I am not use this in dialog)

public void updateTextView(String toThis) {

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setText(toThis);

    //return;
}
Vijay
  • 3,152
  • 3
  • 24
  • 33
0

try like this

int elapsedtime=0;
  boolean isTimerRunning=false;

Timer timerr;

inside onCreate

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
  //declare your textview here;
     timerr=new Timer();
                startTimer();  
}

   /*
     * timer for displaying message bubble
     */
    protected static void startTimer() {
        isTimerRunning = true;
        elapsedtime = 0;
        // recordingseek.setProgress(0);

        timerr.scheduleAtFixedRate(new TimerTask() {
            public void run() {

                 // increase every sec

elapsedtime++;


                    mmHandler.obtainMessage(1).sendToTarget();
                    System.out.println("recording time" + elapsedtime);

                if(elapsedtime==50)
                       timerr.cancel();


            }
        }, 1000, 2000);
    };

    public static Handler mmHandler = new Handler() {

        public void handleMessage(Message msg) {



            textview.setText(elapsedtime);



}
    };




    }
};
Karthika PB
  • 1,373
  • 9
  • 16
  • I used your code, besides, add try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } and the "currentNum.setText(currentNum);" only works when I = 50... – andy Mar 31 '15 at 06:42