Você está na página 1de 6

Stack Overflow

Questions

Tags

Users

sign up
Badges

Unanswered

log in

Ask

Read this post in our app!

How to display text from EditText to ListView?


1

android

I want to display in the listview what I have type in the EditText, so everytime I'm going to input something in the EditText, it will automatically
add in the array adn display it using a listview. Need a help. Thanks.

share

improve this question


user3350065
6 3

Asked
Feb 24 '15 at 15:00

Edited
Feb 24 '15 at 15:06

2 Answers

Have you checked using a ListAdapter? If not, doesnt matter. Here's how it could be done (untested and written by the fly)

order by votes

listview.setAdapter(adapter);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (edt.getText().toString() != null) {
adapter.addItem(edt.getText().toString());
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
ArrayList<String> data = new ArrayList<>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
this.data = (ArrayList)objects;
}
@Override

share

improve this answer


Emanuel Seibold
1,865 1 6

18

Answered
Feb 24 '15 at 15:06

Edited
Feb 24 '15 at 15:12

What if the array is still empty? And it will get string from what i have entered in the edittext? user3350065 Feb 24 '15 at 15:09
Adapter also supports Lists, not only arrays. Using empty list should work. After adding a value to the list, call adapter.notifyDataSetChanged(). Marius Feb 24 '15
at 15:12
modified. now you have null entries and it will be added. Emanuel Seibold Feb 24 '15 at 15:12

add a comment

You can use the ArrayAdapter to handle the list of String you want to display.
On the Button click, you can then add the EditText content to the adapter and then notifyDataSetChanged() the adapter so it "redraws"
itself and finally clear the EditText content.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
EditText input;
ListView list;
ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// android:id="@+id/input"
input = (EditText) findViewById(R.id.input);

With a layout similar to:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="addToList" />
</LinearLayout>

share

improve this answer


Gorcyn
1,681

1 8 14

Answered
Feb 24 '15 at 15:19

Edited
Feb 24 '15 at 15:24

It worked! Thanks! user3350065 Feb 24 '15 at 15:28


One last thing, can I add any elements like images and other text in the listview that can't be modified when the user enter the text? I mean everytime the user enter
a text, it will also display the image or the text. user3350065 Feb 24 '15 at 15:31
You can, but you will have to create your own adapter to customize the layout used for each "line" and fill these lines with the right data. Check this sample :
vogella.com/tutorials/AndroidListView/article.html#adapterown Gorcyn Feb 24 '15 at 15:35
Do not forget to validate an answer so others know your problem is solved. Mine or @Emanuel Seibold's, as I see it, it's a working solution too. Gorcyn Feb 24 '15

at 15:38

add a comment

Your Answer

log in

or
Name

Email

Post Your Answer

By posting your answer, you agree to the privacy policy and terms of service.

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Post Your Answer

Você também pode gostar