Reputation: 13
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
Reputation: 752
You didn't set adapter to listview
add this line
listView.setAdapter(arrayAdapter);
Upvotes: 1
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
Reputation: 41
Use this instead
ArrayApater adapter=new ArrayAdapter(getApplicationContext,android.R.layout.simple_list_item_1,notes);
listView.setAdapter(adapter);
Upvotes: 0
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
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
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