Escolar Documentos
Profissional Documentos
Cultura Documentos
Questions
Tags
Users
sign up
Badges
Unanswered
log in
Ask
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
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
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);
<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
1 8 14
Answered
Feb 24 '15 at 15:19
Edited
Feb 24 '15 at 15:24
at 15:38
add a comment
Your Answer
log in
or
Name
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