无法从Cloud Firestore设置编辑文本中的文本。

huangapple go评论91阅读模式
英文:

Not able to set text in edit text from cloud firestore?

问题

[![这是edit_note活动的图像这里的内容文本没有显示][1]][1]打开笔记后我看到了我的笔记标题和内容的文本视图我想编辑笔记但是编辑笔记活动出现了问题它只显示标题的文本在编辑框中但在此之下内容文本没有显示我尝试了许多方法比如我尝试检查问题在哪里我放入了标题的数据但它也没有显示标题也就是说上面的标题编辑框可以显示标题数据和内容数据但第二个编辑框却不能显示标题数据和内容数据

这是`notes_open.class`:

```java
public class notes_open extends AppCompatActivity {
    Intent data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes_open);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);

        data = getIntent();
        TextView MyTitle = findViewById(R.id.notesopentitle);
        TextView MyContent = findViewById(R.id.notesopencontent);
        MyContent.setMovementMethod(new ScrollingMovementMethod());

        MyTitle.setText(data.getStringExtra("ourtitle"));
        MyContent.setText(data.getStringExtra("ourcontent"));

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.notesopen, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.edit_note:
                Intent i = new Intent(getApplication(), edit_note.class);
                i.putExtra("ourtitle", data.getStringExtra("ourtitle"));
                i.putExtra("ourcontent", data.getStringExtra("ourcontent"));
                startActivity(i);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

以下是edit_note.class

public class edit_note extends AppCompatActivity {
    Intent data;
    EditText edit_title, edit_note;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_note);
        data = getIntent();

        edit_title = findViewById(R.id.edit_ttile);
        edit_note = findViewById(R.id.edit_note);
        String edititle = data.getStringExtra("ourtitle");
        String editcontent = data.getStringExtra("ourcontent");
        try {
            edit_title.setText(edititle);
            edit_note.setText(editcontent);
        } catch (NullPointerException ignore) {
        }
    }
}

以下是edit_note.xml的部分内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".edit_note">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        tools:ignore="MissingConstraints">

        <!-- Toolbar and other content inside AppBarLayout -->

    </com.google.android.material.appbar.AppBarLayout>

    <EditText
        android:id="@+id/edit_ttile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Title"
        android:layout_below="@id/appBarLayout2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="56dp" />

    <EditText
        android:id="@+id/edit_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Content"
        android:layout_below="@id/edit_ttile"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="56dp" />

    <!-- Other UI elements -->

</RelativeLayout>

请注意,我只是按照你的要求提供了代码的翻译,没有提供其他内容。如果你有任何关于这段代码的问题,请随时问我。
<details>
<summary>英文:</summary>
[![This is Image of edit_note activity. Here content text is not showing][1]][1]After opening notes I see textview of my note&#39;s title and content. I want to edit notes. But there is a problem with the edit note activity. It shows only the text of title in edittext. But below this content text not shown. I tried many ways like I tried to check what&#39;s a problem I put data of title. But it is also not showing the title. Means upper title edit text can show title data and content data but second edit text doesn&#39;t show title data and content data.
This is notes_open.class  
public class notes_open extends AppCompatActivity {
Intent data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_open);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
data= getIntent();
TextView MyTitle = findViewById(R.id.notesopentitle);
TextView MyContent= findViewById(R.id.notesopencontent);
MyContent.setMovementMethod(new ScrollingMovementMethod());
MyTitle.setText(data.getStringExtra(&quot;ourtitle&quot;));
MyContent.setText(data.getStringExtra(&quot;ourcontent&quot;));
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, &quot;Replace with your own action&quot;, Snackbar.LENGTH_LONG)
.setAction(&quot;Action&quot;, null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater= getMenuInflater();
getMenuInflater().inflate(R.menu.notesopen, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case  android.R.id.home:
onBackPressed();
case R.id.edit_note:
Intent i =  new Intent(getApplication(),edit_note.class);
i.putExtra(&quot;ourtitle&quot;,data.getStringExtra(&quot;ourtitle&quot;));
i.putExtra(&quot;ourcontent&quot;,data.getStringExtra(&quot;ourcontent&quot;));
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
}
Below is edit_note.class
public class edit_note extends AppCompatActivity {
Intent data;
EditText edit_title,  edit_note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_note);
data =  getIntent();
edit_title =  findViewById(R.id.edit_ttile);
edit_note =  findViewById(R.id.edit_note);
String edititle =  data.getStringExtra(&quot;ourtitle&quot;);
String editcontent =  data.getStringExtra(&quot;ourcontent&quot;);
try {
edit_title.setText(edititle);
edit_note.setText(editcontent);
}catch (NullPointerException ignore){}
}
}
Edit note xml
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.edit_note&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/appBarLayout2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:id=&quot;@+id/toolbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;?attr/actionBarSize&quot;
android:background=&quot;?attr/colorPrimary&quot;
app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;EditText
android:id=&quot;@+id/edit_ttile&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;textPersonName&quot;
android:hint=&quot;Title&quot;
android:layout_below=&quot;@id/appBarLayout2&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
tools:layout_editor_absoluteY=&quot;56dp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/edit_content&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:ems=&quot;10&quot;
android:inputType=&quot;textPersonName&quot;
android:hint=&quot;Content&quot;
android:layout_below=&quot;@id/edit_ttile&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintHorizontal_bias=&quot;0.0&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
tools:layout_editor_absoluteY=&quot;56dp&quot; /&gt;
&lt;/RelativeLayout&gt;
[1]: https://i.stack.imgur.com/BqJNJ.png
</details>
# 答案1
**得分**: 2
你的编辑文本赋值是错误的:
```java
edit_note = findViewById(R.id.edit_note);

而应该改为:

edit_note = findViewById(R.id.edit_content);
英文:

Your edittext assigning is wrong :

edit_note =  findViewById(R.id.edit_note);

Instead it needs :

edit_note =  findViewById(R.id.edit_content);

huangapple
  • 本文由 发表于 2020年10月27日 13:03:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64548559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定