Jackson Ennis
Jackson Ennis

Reputation: 13

ListView is not showing up on app

Image Here

A quick problem, my Listview is not showing up in my app. I just deleted and redownloaded android studios, so I'm on the newest version.

public class ToDoList extends AppCompatActivity {

    ArrayList<String> notes = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_list);

        ListView listView = (ListView) findViewById(R.id.listView);
        notes.add("Here's an example");
        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(arrayAdapter);

    }
}

Upvotes: 1

Views: 1707

Answers (6)

SahdevRajput74
SahdevRajput74

Reputation: 752

You didn't set adapter to listview

add this line

listView.setAdapter(arrayAdapter);

Upvotes: 1

Sanjay Chauhan
Sanjay Chauhan

Reputation: 903

You must have to pass your arrayList to arrayAdapter, then after set your arrayAdapter in listview.

Use Like This

 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notes );
 listView.setAdapter(arrayAdapter);

Upvotes: 0

DERRICK NGATIA
DERRICK NGATIA

Reputation: 41

Use this instead

ArrayApater adapter=new ArrayAdapter(getApplicationContext,android.R.layout.simple_list_item_1,notes);
listView.setAdapter(adapter);

Upvotes: 0

Vivek Pagar
Vivek Pagar

Reputation: 29

1.Define a new Adapter as follow

First parameter - Context

Second parameter - Layout for the row

Third parameter - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_1, android.R.id.text1, values);

2. Assign adapter to ListView

    listView.setAdapter(adapter); 

Upvotes: 0

Nabeel Nazir
Nabeel Nazir

Reputation: 430

You should have to use this, pass notes to your adatpter

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, notes);
listview.setAdapter(arrayAdapter);

Upvotes: 0

Gowthaman M
Gowthaman M

Reputation: 8272

You have to pass your arrayList to arrayAdapter, then set your arrayAdapter in your listview.

Use this

    ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,notes);
    listView.setAdapter(arrayAdapter);

Upvotes: 1

Related Questions