Reputation: 183
I have two EditText's, title input and desc input.
final EditText naslov_comm = view.findViewById(R.id.naslov_comm);
final EditText opis_comm = view.findViewById(R.id.opis_comm);
final String inputnaslov = naslov_comm.getText().toString();
final String inputopis = opis_comm.getText().toString();
objavicom_btn = view.findViewById(R.id.objavicom_btn);
objavicom_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
sendNetworkRequest(id_post, user_pref, commentDTO_a);
}
});
Everytime my button is clicked, it's supposed to take those inputs, but it returns empty string
inputnaslov=""
inputopis=""
Here is my xml file
<?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="com.example.hrle.android_portal.ReadPostActivity"
android:background="?android:windowBackground"
android:id="@+id/arpfc">
<EditText
android:id="@+id/naslov_comm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Unesite naslov"
android:inputType="text"
/>
<EditText
android:id="@+id/opis_comm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/naslov_comm"
android:hint="Unesite opis"
android:inputType="text" />
<Button
android:id="@+id/objavicom_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/opis_comm"
android:layout_alignParentEnd="true"
android:text="Objavi"
/>
Could final be causing this problem, or maybe android:inputType="text" in my xml?
Upvotes: 2
Views: 76
Reputation: 58934
What you did is, you get value from EditText when it was blank, then you are further using that blank string. Correct way is to get text of EditText when user clicks on button.
final EditText naslov_comm = view.findViewById(R.id.naslov_comm);
final EditText opis_comm = view.findViewById(R.id.opis_comm);
findViewById(R.id.objavicom_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String inputnaslov = naslov_comm.getText().toString();
final String inputopis = opis_comm.getText().toString();
CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
sendNetworkRequest(id_post, user_pref, commentDTO_a);
}
});
Update
Could final be causing this problem, or maybe android:inputType="text" in my xml?
No! It will not make problem, it is just input type like if you set it number, user will see numbers keyboard and can enter numbers only to editext.
Upvotes: 1
Reputation: 75788
You should add getText().toString()
into onClick(View view)
Section just.
objavicom_btn = view.findViewById(R.id.objavicom_btn);
objavicom_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String inputnaslov = naslov_comm.getText().toString();
final String inputopis = opis_comm.getText().toString();
CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
sendNetworkRequest(id_post, user_pref, commentDTO_a);
}
});
Upvotes: 2
Reputation: 82958
The place where you are reading values of edittext is not correct. At that time your editText must have empty value (as that is declaration block where you are trying to read a dynamic value).
So when you want to read a value dynamically, best place is to read it on click of button.
Move value reading code in onclick on buttons
inputnaslov = naslov_comm.getText().toString();
inputopis = opis_comm.getText().toString();
Like below
objavicom_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Read latest value of edittexts
inputnaslov = naslov_comm.getText().toString();
inputopis = opis_comm.getText().toString();
CommentDTO commentDTO_a = new CommentDTO(inputnaslov, inputopis);
sendNetworkRequest(id_post, user_pref, commentDTO_a);
}
});
Upvotes: 1