android进度条不显示 [英] android progress bar not showing

查看:97
本文介绍了android进度条不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,那么我在这里想念什么?我试图在按下按钮时启动一个圆形进度条,并在方法完成后完全停止进度条,或者仅在任务完成后才停止显示它,这是因为在其他教程中我似乎做得很好.

ok so what am i missing here? i am trying to start a circular progress bar in a button press and stop it after methods finish the progress bar does not display at all or displays only when tasks are finished weird because it seems in other tutorials i am doing right.

public class contacts extends AppCompatActivity {
Cursor cursor;
Cursor cursor2;
ArrayList<String> vCard ;
String vfile;
 int a;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    AdView mAdView = (AdView) findViewById(R.id.adView2);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
     final ProgressBar pro = (ProgressBar) findViewById(R.id.pb);
    pro.setVisibility(View.GONE);

    TextView text = (TextView) findViewById(R.id.textView5);
    cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    a = cursor2.getCount();
    StringBuilder sb = new StringBuilder();
    sb.append("נמצאו");
    sb.append(" ");
    sb.append(a);
    sb.append(" ");
    sb.append("אנשי קשר");

    String b1 = sb.toString();
    text.setText(b1);
    // Log.d("printwtfbro",String.valueOf(a));
    Button btn2 = (Button) findViewById(R.id.button6);



    btn2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {





            //vfile = "Contacts" + "_" + System.currentTimeMillis() + ".vcf";
            vfile = "גיבוי אנשי קשר" + ".vcf";
            /**This Function For Vcard And here i take one Array List in Which i store every Vcard String of Every Conatact
             * Here i take one Cursor and this cursor is not null and its count>0 than i repeat one loop up to cursor.getcount() means Up to number of phone contacts.
             * And in Every Loop i can make vcard string and store in Array list which i declared as a Global.
             * And in Every Loop i move cursor next and print log in logcat.
             * */

            try {

              pro.setVisibility(View.VISIBLE);
                //pro.setProgress(30);
                getVcardString();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        private void getVcardString() throws IOException {
            // TODO Auto-generated method stub
            //ProgressBar pro = (ProgressBar)findViewById(R.id.pb);

           // ProgressBar pro = (ProgressBar) findViewById(R.id.pb1);
            vCard = new ArrayList<String>();  // Its global....
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null && cursor.getCount() > 0) {
                int i;
                String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
                FileOutputStream mFileOutputStream = new FileOutputStream(storage_path, false);
                cursor.moveToFirst();
                for (i = 0; i < cursor.getCount(); i++) {

                    get(cursor);
                    Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
                    cursor.moveToNext();

                    mFileOutputStream.write(vCard.get(i).toString().getBytes());
                }
                mFileOutputStream.close();
                cursor.close();
                pro.setVisibility(View.GONE);
            } else {
                Log.d("TAG", "No Contacts in Your Phone");
            }
        }

        private void get(Cursor cursor2) {
            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
            AssetFileDescriptor fd;
            try {
                fd = getContentResolver().openAssetFileDescriptor(uri, "r");

                FileInputStream fis = fd.createInputStream();
                byte[] buf = new byte[(int) fd.getDeclaredLength()];
                fis.read(buf);
                String vcardstring = new String(buf);
                vCard.add(vcardstring);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
}}

xml文件

 <TextView
    android:id="@+id/textView5"
    style="@style/Widget.AppCompat.TextView.SpinnerItem"
    android:layout_width="247dp"
    android:layout_height="41dp"
    android:layout_marginTop="8dp"
    android:textColor="@color/common_google_signin_btn_text_dark_focused"
    android:textSize="24sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<ProgressBar
    android:id="@+id/pb"
    style="?android:attr/progressBarStyle"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/adView2"
    app:layout_constraintTop_toBottomOf="@+id/button6"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintVertical_bias="0.168" />

推荐答案

请尝试在其他线程中运行getVcardString()函数.完成该功能后,请使用处理程序并从此处运行进度栏的可见性更改.

Please try running getVcardString() function in a different thread. Once you are completed with that function, use a handler and run the visibilty change of progress bar from there.

类似的东西:

pro.setVisibility(View.VISIBLE);
    new Thread(new Runnable() {
        @Override
        public void run() {
            getVcardString();
        }
    }).start();

然后通过getVcardString()函数:

Then from getVcardString() function :

    private void getVcardString(){
    ....

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            pro.setVisibility(View.GONE);
        }
    });
}

这篇关于android进度条不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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