繁体   English   中英

android进度条不显示

[英]android progress bar not showing

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

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()函数。 完成该功能后,请使用处理程序并从此处运行进度栏的可视性更改。

就像是 :

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);
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM