0

I'm trying to populate a list view using data taken from an edit text. The process is taking the data from the edit text and putting them in an ArrayList then using an ArrayAdapter using this ArrayList to populate the list view. What I've done so far is:

public class MainActivity extends AppCompatActivity {

    List<String> list=new ArrayList<String>();
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        et=(EditText)findViewById(R.id.edittext);
        final ListView lv = (ListView) findViewById(R.id.lv);
        et.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction()!=KeyEvent.ACTION_DOWN)
                    return true;
                if(KeyEvent.KEYCODE_ENTER == keyCode) {
                    list.add(et.getText().toString());
                    System.out.println(list);
                    ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.main_layout ,list);
                    lv.setAdapter(adapter);
                    return true;
                }
                return false;
            }
        });
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <ListView
        android:id="@+id/lv"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/edittext"
        android:layout_marginStart="13dp"
        android:layout_marginTop="19dp"
        android:visibility="visible">
    </ListView>

</RelativeLayout>

This returns

"java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView"

I tried adding an empty TextView to my XML. When I run the code, the new textview is added with an edittext to the list view, but only one time. So 2 errors occurring here. The edittext being added and only works once.

enter image description here

1 Answer 1

1

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,R.layout.main_layout ,list);

main_layout layout should be textview layout like below.

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"/>

or you can use androids simple textview like below.

ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1 ,list);
Sign up to request clarification or add additional context in comments.

1 Comment

Totally forgot I could use the android's views. Thanks for the answer, marked it as correct!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.