1
votes

In my App i have added Observable method in menuitemclicked but it's working when i'm clicking it twice . on Single press. method is not working but button is working fine any suggestion for this or any changes in method .

case R.id.upload:

fetch() break;

io.reactivex.Observable fetchObservable() {

return io.reactivex.Observable.create(new ObservableOnSubscribe<Data>() {
    @Override
    public void subscribe(ObservableEmitter<Data> emitter) throws Exception {


        try {


            final Cursor cursor = getActivity().getContentResolver().query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{"display_name", "data1"}, null, null, null);


            //  final Map contact =new HashMap();


            while (cursor.moveToNext()) {

                Data data = new Data();


                try {

                    String name = cursor.getString(cursor.getColumnIndex(Display_name));
                    String number = cursor.getString(cursor.getColumnIndex(Data1));
                    data.setContact_name(name);
                    data.setNumber(number);

                    Thread.sleep(60);

                    emitter.onNext(data);

                } catch (Exception e) {

                    Toast.makeText(context, "not insert , Toast.LENGTH_SHORT).show();
                }
            }

            emitter.onComplete();
        }catch (Exception e){

            emitter.onError(e);
        }

        }


});

// fetching method code

void fetch(){

fetchObservable().subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer() { @Override public void onSubscribe(Disposable d) {

      }

      @Override
      public void onNext(Data data) {


          data.Contact_name=data.getContact_name();
          data.number=data.getNumber();


              String name=  data.Contact_name;
              String number=   data.number;





          }




              count++;
              textprogress.setText(String.valueOf(count));







          }

      }

      @Override
      public void onError(Throwable e) {

      }

      @Override
      public void onComplete() {

      }
  });

}

1

1 Answers

1
votes

Because u are not Running your UI in main thread like this

new Handler(Looper.getMainLooper()).post(new Runnable() {
                 @Override
                 public void run() {

                     textprogress.setVisibility(View.VISIBLE);
                     textprogress.setVisibility(View.VISIBLE);

                 }
             });