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

时间:2017-10-17 23:22:46

标签: android firebase firebase-realtime-database

我知道如果我想创建一个新行,我必须在需要创建该行的任何地方插入\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;
    }

}

3 个答案:

答案 0 :(得分:3)

您可以输入特殊字符序列,并在检索数据后将其替换。 Firebase数据库不允许\ character。

所以你可以放入数据库: Hello _b world

检索后,用\ n

替换此_b

答案 1 :(得分:0)

在firebase中,将换行命令存储为“ _n”(不带引号)。然后在onBindViewHolder中执行此操作

if(holder.name.getText().toString().contains("_n")){
  String newName = name.getText.replace("_n","\n");
  holder.name.setText(newName);  
  }

答案 2 :(得分:0)

Firestore自动将\n转换为\\n。因此,如果您想在Firestore中使用\n,请在Android中将\\n转换为\n

Firestore data

textView.text = text.replace("\\n", "\n")

New line displayed in android

奖金

如果使用数据绑定,则可以使用此绑定适配器

@JvmStatic
@BindingAdapter("text_parse_new_line")
fun parseNewLine(textView: TextView, text: String?) {
    textView.text = text?.replace("\\n", "\n")
}

在布局文件中使用

            <TextView
                android:id="@+id/how_to_claim"
                text_parse_new_line="@{viewModel.listing.howToClaim}" />