gpt4 book ai didi

android - 从 firebase 数据库检索数据到 textview 时如何创建换行符?

转载 作者:行者123 更新时间:2023-11-29 16:49:25 24 4
gpt4 key购买 nike

我知道如果我想创建一个新行,我必须在需要创建该行的地方插入 \n。但我得到的是没有我在 firebase 数据库中已有的行的文本。有人可以告诉我如何解决这个问题。例如,如果我在数据库中有 hello\n world。我把它理解为 hello world。当我真的应该把它当作

hello
world

我是否需要更改 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();
}

这是我的适配器

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;
}

}

最佳答案

您可以放置​​特殊字符序列并在检索数据后替换它。Firebase 数据库不允许使用\字符。

所以你可以在数据库中输入:Hello _b world

并且在检索后将此 _b 替换为\n

关于android - 从 firebase 数据库检索数据到 textview 时如何创建换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com