[英]How to create newline when retrieving data from firebase database into textview?
i know that if i want to create a new line, i have to insert \\n
wherever i need to create the line.我知道如果我想创建一个新行,我必须在需要创建该行的任何地方插入
\\n
。 But what i get is the text without the lines that i already have in firebase database.但是我得到的是没有我在 firebase 数据库中已有的行的文本。 can someone tell me how to fix that.
有人可以告诉我如何解决这个问题。 so for example if i have in the database hello \\n world.
例如,如果我在数据库中有 hello \\n world。 i get it as
hello world
.我把它当作
hello world
。 when i really should get it as当我真的应该得到它时
hello
world
will i need to change something in firebase or in my source code?我需要更改 firebase 或源代码中的某些内容吗?
this is how i get the data from firebase这就是我从 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;
}
}
You can put special characters sequence and replace it after retrieving the data.您可以放置特殊字符序列并在检索数据后替换它。 Firebase database doesn't permit \\ character.
Firebase 数据库不允许使用 \\ 字符。
So you can put in the database: Hello _b world这样就可以在数据库中放入: Hello _b world
And after retrieving you replace this _b with \\n检索后,您将此 _b 替换为 \\n
Firestore automatically converts \\n
into \\\\n
. Firestore 会自动将
\\n
转换为\\\\n
。 So if you want to use \\n
in Firestore, convert \\\\n
into \\n
in android.因此,如果您想在 Firestore 中使用
\\n
,请将\\\\n
转换为 android 中的\\n
。
textView.text = text.replace("\\n", "\n")
Bonus
If you use data binding, you can use this binding adapter如果使用数据绑定,则可以使用此绑定适配器
<TextView
android:id="@+id/how_to_claim"
text_parse_new_line="@{viewModel.listing.howToClaim}" />
In layout file use在布局文件中使用
<TextView android:id="@+id/how_to_claim" text_parse_new_line="@{viewModel.listing.howToClaim}" />
In firebase, store the newline command as "_n" without the quotes.在 firebase 中,将换行命令存储为不带引号的“_n”。 Then in onBindViewHolder do this
然后在 onBindViewHolder 中执行此操作
if(holder.name.getText().toString().contains("_n")){
String newName = name.getText.replace("_n","\n");
holder.name.setText(newName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.