2016-12-27 20 views
0

私は、ユーザーがインターネットに接続してもFirebaseから解析された情報(この場合、各オブジェクトには3つの文字列変数があります)は、何らかの方法(sharedPreferencesまたはファイルまたは他の方法を使用して)で保存することができます。次に、ユーザーがインターネット接続なしでアプリを開くと、この既に解析されたデータが表示され、正常に動作するはずです。オフラインで閲覧するためにFirebaseから解析された情報を保存する

このため、私はディスクの永続性について知りましたが、動作していません。ここに私のコードは

package in.protechlabz.www.yavatmalindicatorserver; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import com.firebase.client.DataSnapshot; 
import com.firebase.client.Firebase; 
import com.firebase.client.FirebaseError; 
import com.firebase.client.ValueEventListener; 
import com.google.android.gms.ads.AdView; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.storage.StorageReference; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Nikesh on 23/12/2016. 
*/ 
public class ContactListActivity extends AppCompatActivity { 

    private List<ContactData> generalList = new ArrayList<ContactData>(); 

    Intent intent; 
    private int listItemSelector=0; // This variable will be used to store information obtained from 
    // intent regarding which listview to be loaded 
    private AdView mAdView2; 
    Firebase myFirebaseRef; 
    private ArrayAdapter<ContactData> listItemAdapter; 
    private StorageReference mStorageRef; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.directory_list); 

     /* Admob related important code*/ 
     mAdView2 = (AdView) findViewById(R.id.adView2); 
     com.google.android.gms.ads.AdRequest adRequest = new com.google.android.gms.ads.AdRequest.Builder().build(); 
     mAdView2.loadAd(adRequest); 

     Firebase.setAndroidContext(this); 


     // Extract information from intent for listItemSelector 
     Bundle extras = getIntent().getExtras(); 

     if(extras != null) { 
      listItemSelector = extras.getInt("ListPosition"); 
     } 

     //if(isNetworkAvailable()) { 
      final ProgressDialog dialog = new ProgressDialog(ContactListActivity.this); 
      dialog.setMessage("Loading Data..."); 
      dialog.setCanceledOnTouchOutside(false); 
      dialog.show(); 

      //Adding information to individual lists 
     /*Firebase related code*/ 
      myFirebaseRef = new Firebase("https://yavatmalindicatorserver.firebaseio.com"); 
      FirebaseDatabase.getInstance().setPersistenceEnabled(true); 


      myFirebaseRef.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot snapshot) { 

        generalList.clear(); 
        switch (listItemSelector) { 

         case 1: 
          for (DataSnapshot eachContact : snapshot.child("Hotels and Lodges").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 2: 
          for (DataSnapshot eachContact : snapshot.child("Banks").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 3: 
          for (DataSnapshot eachContact : snapshot.child("Tours and Travels").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 4: 
          for (DataSnapshot eachContact : snapshot.child("Function Halls").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 5: 
          for (DataSnapshot eachContact : snapshot.child("Hospitals").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 6: 
          for (DataSnapshot eachContact : snapshot.child("Educational Institutes").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 7: 
          for (DataSnapshot eachContact : snapshot.child("Medicals").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 8: 
          for (DataSnapshot eachContact : snapshot.child("Government Offices").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 9: 
          for (DataSnapshot eachContact : snapshot.child("Press").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
        } 
          break; 
         case 10: 
          for (DataSnapshot eachContact : snapshot.child("Railways (Dhamangaon)").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 

         // From here emergency list 
         case 11: 
          for (DataSnapshot eachContact : snapshot.child("Police Station").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 12: 
          for (DataSnapshot eachContact : snapshot.child("Fire Brigade").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 13: 
          for (DataSnapshot eachContact : snapshot.child("Ambulance").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 14: 
          for (DataSnapshot eachContact : snapshot.child("Blood Banks").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 
         case 15: 
          for (DataSnapshot eachContact : snapshot.child("Snake Friends").getChildren()) { 
           ContactData tempData = eachContact.getValue(ContactData.class); 
           generalList.add(tempData); 
          } 
          break; 


        } 


        listItemAdapter.notifyDataSetChanged(); 
        dialog.hide(); 

       } 

       @Override 
       public void onCancelled(FirebaseError error) { 
       } 
      }); 
     //}else { 

      // Toast.makeText(this, "Sorry No Internet Connection", 
      //  Toast.LENGTH_SHORT).show(); 
      //finish(); 
     //} 

     listItemAdapter = new ContactDataAdapter(this,R.layout.eachcontact_row,generalList); 
     ListView myContactList = (ListView) findViewById(R.id.directoryList); 

     //For card view 
     myContactList.addHeaderView(new View(this)); 
     myContactList.addFooterView(new View(this)); 

     myContactList.setAdapter(listItemAdapter); 

     myContactList.setOnItemClickListener(
       new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         ContactData singleItem = (ContactData) parent.getItemAtPosition(position); 
         String number = singleItem.getTelephone(); 

         // Parse only one number if multiple numbers present 
         int indexOfComma = number.indexOf(","); 

         if(indexOfComma != -1) { 
          number = number.substring(0,indexOfComma); 
         } 

         intent = new Intent(Intent.ACTION_DIAL); 
         intent.setData(Uri.parse("tel:" + number)); 
         startActivity(intent); 
        } 
       } 
     ); 




    } 

    /*public boolean isNetworkAvailable(final Context context) { 
     final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 
     return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected(); 
    }*/ 
    private boolean isNetworkAvailable() { 
     ConnectivityManager connectivityManager 
       = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
    } 
} 

助けてください。

+1

幸運は、あなたはこれを見たことがありますか? –

+1

はい私は正確にその記事を見て、私のコードにも含まれていますが、機能していません。 –

+0

'.keepSynced(true);キャッシュに保存したいリファレンスに追加するか、自分のアプリ用に独自のローカルDBを作成します。 –

答えて

1

この目的のためのシンプルで便利なライブラリがあります。それはTinyDBと呼ばれています。プロジェクトに追加したら、TinyDBのインスタンスでputListObject()と呼ぶだけで、データが保存されます。 - https://firebase.google.com/docs/database/android/offline-capabilities - その

関連する問題