如何进步吧

时间:2014-01-15 17:49:33

标签: java android eclipse

我在一个活动中有两个活动我做了停止删除过程并在活动中显示两个,这个过程需要很长时间,如果单词量越来越多,我想做一个进度条让用户不要混淆。 在停用词删除过程完成之前如何制作进度条? 我不得不把它放在编码的地方? 我是android编程的初学者,希望有人能帮助我。

这是我在第一项活动中点击按钮的编码。

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId()==R.id.button6){
        Intent intent = new Intent(this, PreposisiRemoval.class);

        String test = ((TextView)findViewById(R.id.textView7)).getText().toString();
        String[] preposisi = {"akibat", "atas", "bagai", "bagi", "berkat", "dalam", "dari", "demi", "dengan", "di", "hingga", "karena", "ke", "kecuali", "lewat", "oleh", "pada", "sampai", "sejak", "seperti", "tanpa", "tentang", "untuk"};
        StringBuilder resultFilter = new StringBuilder();

        Scanner fip1 = new Scanner(test);
            while (fip1.hasNext()){
                    int flag = 1;
                    String s1 = fip1.next();
                    for (int i = 0;i<preposisi.length; i++){
                        if (s1.equals(preposisi[i])){
                            flag=0;
                        }
                        }   
                        if(flag!=0){
                            System.out.println(s1);
                            resultFilter.append(s1+'\n');
                        }

                        Intent intent2 = getIntent();
                                String count = intent2.getStringExtra("result2");

                    intent.putExtra("resultFilter", resultFilter.toString());
                    intent.putExtra("count", count);
                    startActivity(intent);
                    }
            }
        }

2 个答案:

答案 0 :(得分:0)

试试这段代码

   public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId()==R.id.button6){
    Intent intent = new Intent(this, PreposisiRemoval.class);

    String test = ((TextView)findViewById(R.id.textView7)).getText().toString();
    String[] preposisi = {"akibat", "atas", "bagai", "bagi", "berkat", "dalam", "dari", "demi", "dengan", "di", "hingga", "karena", "ke", "kecuali", "lewat", "oleh", "pada", "sampai", "sejak", "seperti", "tanpa", "tentang", "untuk"};
    StringBuilder resultFilter = new StringBuilder();

    Scanner fip1 = new Scanner(test);
    ProgressDialog dialog = ProgressDialog.show(MainActivity.this,"Wait Dialog","Please wait...", true, false);
        while (fip1.hasNext()){
                int flag = 1;
                String s1 = fip1.next();
                for (int i = 0;i<preposisi.length; i++){
                    if (s1.equals(preposisi[i])){
                        flag=0;
                    }
                    }   
                    if(flag!=0){
                        System.out.println(s1);
                        resultFilter.append(s1+'\n');
                    }
                  dialog.cancel();
                    Intent intent2 = getIntent();
                            String count = intent2.getStringExtra("result2");

                intent.putExtra("resultFilter", resultFilter.toString());
                intent.putExtra("count", count);
                startActivity(intent);
                }
        }
    }

答案 1 :(得分:0)

您可以使用AsyncTask执行此类操作,或者只在工作开始之前放入ProgressDialog.show()并在完成时关闭。对于asyncTask,代码如下。

 private class doLoginTask extends AsyncTask<String, Integer, String>{

    private ProgressDialog pb=null;
    //private String response=null;
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
                    //Progress show
        pb=ProgressDialog.show(ClubLoginActivity.this, "", "Processing");
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        //add your work here..

        return myWork();
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pb.dismiss();
                    //code after dismiss it.
                    Intent intent2 = getIntent();
    String count = intent2.getStringExtra("result2");
    Intent intent = new Intent(this, PreposisiRemoval.class);

    intent.putExtra("resultFilter", resultFilter.toString());
    intent.putExtra("count", result);
    startActivity(intent);

    }
}
    String myWork(){

    String test = ((TextView)findViewById(R.id.textView7)).getText().toString();
    String[] preposisi = {"akibat", "atas", "bagai", "bagi", "berkat", "dalam", "dari", "demi", "dengan", "di", "hingga", "karena", "ke", "kecuali", "lewat", "oleh", "pada", "sampai", "sejak", "seperti", "tanpa", "tentang", "untuk"};
    StringBuilder resultFilter = new StringBuilder();

    Scanner fip1 = new Scanner(test);
        while (fip1.hasNext()){
                int flag = 1;
                String s1 = fip1.next();
                for (int i = 0;i<preposisi.length; i++){
                    if (s1.equals(preposisi[i])){
                        flag=0;
                    }
                    }   
                    if(flag!=0){
                        System.out.println(s1);
                        resultFilter.append(s1+'\n');
                    }


}
        return resultFilter.toString();
}