ListView is not showing up on app - android

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);
}
}

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);

You didn't set adapter to listview
add this line
listView.setAdapter(arrayAdapter);

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);

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);

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

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);

Related

Android: Can i set custombase adapter to a textview

This is how i populate the data to
adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
is there any option for me to do for setting textview to a custom base adapter
like below?
textview.setAdapter(adapter);

xml string-array to listView not working

public class Menu extends Activity {
String[] categories;
ListView lv;
Cursor cursor;
Context context;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categories = getResources().getStringArray(R.array.Categories_Array);
lv = (ListView) findViewById(R.id.listViewCategories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
lv.setAdapter(adapter);
}
...
}
I keep getting an "unfortuneately Bible CYB has stopped working" when I run the app
The problem you have is that you are giving your ArrayAdapter a full Activity's layout to inflate. What you want to do is pass it the row's layout it should use for each of the individual rows it should inflate:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, // or other basic row layout
getResources().getStringArray(R.array.Categories_Array));
You have set the activity_main.xml to the activity and use the same in the constructor of ArrayAdapter. To display the a single string in each row
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_list_item_1,
categories);
where simple_list_item_1.xml is the layout from the android framework
http://androidxref.com/4.4.3_r1.1/xref/frameworks/base/core/res/res/layout/simple_list_item_1.xml
And the constructor
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters context The current context. resource The resource ID for a
layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Instead of using a array adapter try using a list adapter.. it wud give it a custom layout u designed ..
lv = (ListView) findViewById(R.id.listViewCategories);
ListAdapter adapter = new SimpleAdapter(this,
/*id for your custom layout for ex: R.layout.activity_main*/,
getResources().getStringArray(R.array.Categories_Array),/*Here write the id of the text view where u want to display the info.. forex: R.id.tvExample*/);
lv.setListAdapter(adapter);

how to correctly call setListAdapter

I've searched how to solve this problem a lot, but i did not get any result. the only help that i need is, please take a look at this question asked on stackoverflow and on answer;
here is the question from this link:
"my activity does not extend ListActivity so this line throws an error. setListAdapter(fileList); it is supposed to populate a listview..."
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
the answer to this question is:
Obtain the reference of your ListView using findViewById(int). Then call the method ListView.setAdapter(ListAdapter) on that reference with your adapter as the parameter.
now here is my code:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
setListAdapter(adapter);
}
considering the Q/A, could someone please explain how exactly do i write it in the correct way. Any help would be very much appreciated.
As explained in the linked question and in this question's comments, it goes like this:
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
ListView lv = (ListView)findViewById(R.id.your_listview_id);
lv.setAdapter(adapter);
ListActivity is a specialized Activity that hosts a ListView and has some convenience methods for accessing and manipulating it. However, nothing prevents you from having a ListView in a non-ListActivity Activity, you just lose the convenience methods and will have to write some more code yourself, like above.
laalto is right, but you can also just extend ListActivity in your current activity.
public class YourListActivityClass extends ListActivity {
private ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
setListAdapter(adapter);
}
}
we can do listview in AppCompatActivity
String[] one = {"dsk", "jskj", "ddd", "dld"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_row, R.id.list_row_name, one);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
setListAdapter is undefined because ReminderListActivity doesn't extend ListActivity. So to solve this you need to extend ListActivity.

Array Adapter Issue

I'm trying to input an array into a ListView. I've gotten it to work for a spin box with this code:
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, model.getLocationsArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
When I run the next block of code the program closes unexpectedly. Strangely if I remove model.getLocationsArray() it runs but the view won't update.
ListView listView = (ListView) findViewById(R.id.available_locations_list);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, model.getLocationsArray());
listView.setAdapter(adapter);
Thanks in advance!
model.getLocationsArray() instead this... you can use directly Arraylist object. If you have.Nullpointer Exc. because of from your EditText value cant be added to your ArrayList.

Populating ListView with ArrayAdapter force closes

I am trying to use the following code to populate a ListView using a predefined array of strings:
String[] schedule_names = getResources().getStringArray(R.array.test_schedules);
// Populate the ListView using the array of schedule names
ArrayList<String> als = new ArrayList<String>(Arrays.asList(schedule_names));
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
adapter.add("Test");
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
But it force closes unless I comment out listView.setAdapter(adapter); (which obviously means the ListView isn't populated at all). It seems the reason is a NullPointerException.
Why is this?
This line is wrong...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.listView, als);
...the second parameter of the constructor should not be your ListView, it should be a layout with a TextView.
Try replacing R.id.listView with android.R.layout.simple_list_item_1

Categories

Resources