2017-10-17 61 views
1

我知道如果我想創建一條新線,我必須在需要創建線的地方插入\n。但是我得到的是沒有我已經在firebase數據庫中擁有的行的文本。有人可以告訴我如何解決這個問題。所以例如,如果我在數據庫hello \ n世界。我得到它作爲hello world。當我真的應該得到它作爲如何在從firebase數據庫檢索數據到textview時創建換行符?

hello 
world 

我需要改變一些在firebase或我的源代碼?

這是我從火力

private void getData(){ 
     firebaseDatabaseInstance = FirebaseDatabase.getInstance(); 

     // get reference to 'users' node 
     booksInstance = firebaseDatabaseInstance.getReference("monzmat"); 

     books.clear(); 
     books.addAll(db.getAllBookMonzmat()); 
     adapter = new qamoosAdapter(this, books); 
     gridView.setAdapter(adapter); 


     booksInstance.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       storeData(dataSnapshot); 
      } 

      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       Log.w(TAG, "Failed to read value.", error.toException()); 
      } 
     }); 

     booksInstance.orderByChild("id").addChildEventListener(new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
       qamoosData book = new qamoosData(
         (String) dataSnapshot.child("id").getValue(), 
         (String) dataSnapshot.child("title").getValue(), 
         (String)dataSnapshot.child("content").getValue() 


       ); 
       db.insertMonzmat(book); 
       reloadData(); 
      } 

      @Override 
      public void onChildChanged(DataSnapshot dataSnapshot, String s) { 
       qamoosData book = new qamoosData(
         (String) dataSnapshot.child("id").getValue(), 
         (String) dataSnapshot.child("title").getValue(), 
         (String)dataSnapshot.child("content").getValue() 

       ); 
       db.updateABookMonzmat(book); 
       reloadData(); 
      } 

      @Override 
      public void onChildRemoved(DataSnapshot dataSnapshot) { 
       qamoosData book = new qamoosData(
         (String) dataSnapshot.child("id").getValue(), 
         (String) dataSnapshot.child("title").getValue(), 
         (String)dataSnapshot.child("content").getValue() 
       ); 
       db.deleteABookMonzmat(book.getId()); 
       reloadData(); 
      } 

      @Override 
      public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 




    private void storeData(DataSnapshot snapshot) { 
     books.clear(); 
     for (DataSnapshot alert: snapshot.getChildren()) { 
      qamoosData book = new qamoosData(
        (String)alert.child("id").getValue(), 
        (String)alert.child("title").getValue(), 
        (String)alert.child("content").getValue() 

      ); 
      db.insertMonzmat(book); 
     } 
     books.addAll(db.getAllBookMonzmat()); 
     // Constants.hideDialog(); 

     adapter.notifyDataSetChanged(); 
    } 

獲取數據這是我的適配器

public class qamoosAdapter extends BaseAdapter { 


    Activity activity; 
    ArrayList<qamoosData> arrayList = new ArrayList<>(); 
    ArrayList<qamoosData> filterList = new ArrayList<>(); 

    LayoutInflater mLayoutInflater; 
    DBHandler db; 
    Storage storage; 

    public qamoosAdapter(Activity context, ArrayList<qamoosData> bookDatas){ 
     this.activity = context; 
     this.arrayList = bookDatas; 
     this.db = new DBHandler(context); 
     this.filterList= bookDatas; 
     mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     storage = SimpleStorage.getInternalStorage(context); 
    } 

    @Override 
    public int getCount() { 
     return arrayList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return arrayList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     qamoosAdapter.ViewHolder holder = new qamoosAdapter.ViewHolder(); 
     if (convertView == null) { 
      convertView = mLayoutInflater.inflate(R.layout.fragment_qamoos, null); 



      holder.name = (TextView) convertView.findViewById(R.id.bookName2_makotab_fragment); 


      if(holder.name.getText().toString().contains("\n")){ 


      } 

      convertView.setTag(holder); 

     } else { 
      holder = (qamoosAdapter.ViewHolder) convertView.getTag(); 
     } 

     holder.name.setText(arrayList.get(position).getTitle()); 
     return convertView; 
    } 


    public class ViewHolder{ 

     ImageView image; 

     TextView name; 
    } 

} 
+0

你能後,增加的數據火力地堡和代碼,獲取碼它? –

+0

請添加您的代碼。 –

+0

請看看我添加的代碼 – abdo

回答

2

你可以把特殊的字符序列和檢索數據後更換。 Firebase數據庫不允許\字符。

所以,你可以把數據庫中的:你好_B世界

和檢索你以後再更換此_B \ n

+0

你能告訴我怎麼可以在代碼中做到這一點? – abdo

+0

當你寫數據到數據庫時,寫_b而不是/ n。 (String)dataSnapshot.child(「content」)。getValue()。replace(「_ b」,「/ n」); 檢索到數據後 –

相關問題