ListAdapter error - android

Could someone tell me what is wrong with this piece of code?
It is supposed to be a ListView from an XML file that is then referred to in Java.
Alas, it crashes my application every time it enters the Menu class.
public class Menu extends ListActivity {
String Name_for_classes[] = {"- 1-9 Tabels -", "- 10-19 Tabels -", "- 20-29 Tabels -" };
String Tabel_classes[] = {"First", "Second", "Third"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, Name_for_classes));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
}

Okay, let's assume you have your ListView in an XML file called my_listview.xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview.xml);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> yourAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Name_for_classes);
list.setAdapter(yourAdapter);
}

I'm not sure what is in R.layout.menu, but guessing by the naming its the activity layout. This should be used with setContentView(R.layout.menu) in onCreate. The layout that is passed into the ArrayAdapter is the TextView you are using to populate the listview.

You have forgotten to call the setContentView in your onCreate method, so your listview is not referenced yet.
When you use setContentView, it is equivalent as sayin 'for this activity I want to use the template 'myTemplate.xml'
After that, you have to 'linked' your java ListView attribute to the listview declared in your template.

Related

Why my app containing listView is crashing?

My app contains only 2 files one is MainActivity.java and activity_main.xml file. I wanted to use to listView to display a list of string but for some my app is crashing continuously.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Pandey");
arrayList.add("Akshay");
arrayList.add("Akshay");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
ListView listView=(ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
}
Error
Your app is crashing because you are passing 0 as the second argument to your ArrayAdapter constructor. This is the "layout resource id" that your list will use for each item, so it needs to be something real.
The Android platform comes with some resources for this purpose built in, like android.R.layout.simple_list_item_1. You could try that. Change this line:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
to this instead:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
More information can be found in the developer guide: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
You can't set the second argument of ArrayAdapter to 0. The constructor expects a valid resource. You need to set it to something like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.my_resource_view, arrayList);

How ArrayAdapter really works with ListView?

I'm confused how the actually ArrayAdapter works? As I was testing with ArrayAdapter and read about it that I have to call the notifyDataSetChanged(); on adapter or update the listView's adapter (as listView.setAdapter()) to update the record in ListView.
Now check this code.
public class MainActivity extends AppCompatActivity {
ArrayList<String> list = new ArrayList<>();
ExampleArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.listView);
adapter = new ExampleArrayAdapter(this,list);
listView.setAdapter(adapter);
// Here I'm adding record after the listView.setAdapter(adapter);
// it is working fine.
list.add("Good");
list.add("Bad");
}
public void addData(View view){
// but when I call this method from Button then it doesn't working.
list.add("New Data Added");
}
}
I don't think so there is any difference between these lines.
list.add("Good");
list.add("Bad");
and
list.add("New Data Added");
Both are adding record after the setAdapter();
Then why list.add("New Data Added"); is not working.
After onCreate() by activity lifecycle run onStart() and onResume(). draw is after onCreate(). Therefore 2 items are visible.
addData(View view) runs after view is visible. To refresh values you need at this place adapter.notifyDataSetChanged();

Adding arraylist data to listview

I have to display a list of messages and make it clickable ,i have read ListView andtried to use it,but i have data in something like below code,how will i add to adapter? from this list i will loop and get messages say list.get(i).getMessage(); which has to be displayed and there are multiple messages.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.reminderlist);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.reminderlayout);
setContentView(R.layout.reminderlayout);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
System.out.println("size is >>>"+list.size());
// Binding resources Array to ListAdapter
}
Implement your custom adapter parametrized with GetReminder. Take a look here for example. Than you can do something like this:
ArrayList<GetReminder> list = (ArrayList<GetReminder>) getIntent().getSerializableExtra("reminderList");
ListView listView = findViewById(R.id.listview);
MyCustomAdapter adapter = new MyCustomAdapter(this, 0);
adapter.addAll(list)
or just pass your list as a third parameter in MyCustomAdapter constructor.
You can do
setListAdapter(new ArrayAdapter<GetReminder>(getApplicationContext(), R.layout.reminderlayout,(GetReminder[]) list.toArray()));
considering this code is in onCreate() method of your class that extends ListActivity

Newbie setting up a ListView

I am just starting out using Android, so this is probably a very basic question.
I have created an array called priorityNames. I want to display that in a list and be able to make a selection from that list. At this stage, I cannot get the list to display.
As a starting point I used a short example from windrealm.org tutorials
Any help would be appreciated. Also if anyone can point me to a better example it would be appreciated.
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.array.priorityNames);
mainListView.setAdapter(listAdapter);
}
}
You are passing just an id with R.array.priorityNames.
Use:
getResources().getStringArray(R.array.priorityNames)
You are probably missing a TextView in your xml layout (simplerow.xml) with id:
...android:id="#android:id/text1"...

Android ListView understanding

I have a question about the ListView and how to use it. My Prolem is that my listView is only a part of the view and I am not sure how to do this.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.ListView01);
String[] strings = new String[]{"Test1","Test2"};
ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.ListView01,strings);
myListView.setAdapter(myArrayAdapter);
I think the problem is the "this" in myArrayAdapter!?
The layout resource id you're supposed to pass to ArrayAdapter is a layout that's used to render each item in the list, not the layout for the list itself. Android provides some layout resources for the common cases. Try using:
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);

Categories

Resources