Reputation: 93
i know that if i want to create a new line, i have to insert \n
wherever i need to create the line. But what i get is the text without the lines that i already have in firebase database. can someone tell me how to fix that. so for example if i have in the database hello \n world. i get it as hello world
. when i really should get it as
hello
world
will i need to change something in firebase or in my source code?
this is how i get the data from 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();
}
This is my adapter
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;
}
}
Upvotes: 3
Views: 7880
Reputation: 1873
Firestore automatically converts \n
into \\n
. So if you want to use \n
in Firestore, convert \\n
into \n
in android.
textView.text = text.replace("\\n", "\n")
Bonus
If you use data binding, you can use this binding adapter
@JvmStatic
@BindingAdapter("text_parse_new_line")
fun parseNewLine(textView: TextView, text: String?) {
textView.text = text?.replace("\\n", "\n")
}
In layout file use
<TextView
android:id="@+id/how_to_claim"
text_parse_new_line="@{viewModel.listing.howToClaim}" />
Upvotes: 5
Reputation: 79
In firebase, store the newline command as "_n" without the quotes. Then in onBindViewHolder do this
if(holder.name.getText().toString().contains("_n")){
String newName = name.getText.replace("_n","\n");
holder.name.setText(newName);
}
Upvotes: 0
Reputation: 563
You can put special characters sequence and replace it after retrieving the data. Firebase database doesn't permit \ character.
So you can put in the database:Hello _b world
And after retrieving you replace this _b with \n
Upvotes: 6