android进度条没有显示

时间:2017-05-17 18:12:16

标签: android android-progressbar

好的,我在这里缺少什么?我试图在按下按钮时启动圆形进度条并在方法完成后停止它进度条根本不显示或仅在任务完成时显示奇怪,因为在其他教程中我似乎做得对。

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" />

1 个答案:

答案 0 :(得分:1)

请尝试在另一个线程中运行getVcardString()函数。完成该功能后,使用处理程序并从那里运行进度条的可见性更改。

喜欢的东西:

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

然后从getVcardString()函数:

    private void getVcardString(){
    ....

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