2017-05-17 2 views
0

오케이 내가 여기에 무엇을 놓치고 있습니까? 나는 버튼 누름에서 원형 진행률 표시 줄을 시작하려고합니다. 메소드가 진행 표시 줄을 완료 한 후 중지합니다. 작업 표시 줄이 전혀 표시되지 않거나 다른 튜토리얼에서 보이는 것처럼 작업이 완료 될 때만 표시됩니다.android 진행률 표시 줄이 표시되지 않습니다.

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

다른 스레드에서 getVcardString() 함수를 실행 해보세요. 해당 함수로 작업을 완료하면 핸들러를 사용하여 거기에서 진행 막대의 가시적 인 변경을 실행합니다. 마찬가지로

뭔가 : getVcardString에서 다음

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

() 함수 :

private void getVcardString(){ 
    .... 

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

완벽하게 작동합니다! 감사! –

관련 문제