Ali.Rashidi Ali.Rashidi - 6 months ago 20
Java Question

Error in simple android app list adapter

This is my java code:

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
ListView myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList=(ListView)findViewById(R.id.myList);
List<String> array_list=new ArrayList<String>();
array_list.add("One Option");
array_list.add("Two Option");
array_list.add("Three Option");
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,R.layout.activity_main,array_list);
myList.setAdapter(arrayAdapter);

}

}


and this is my xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myList"
android:drawSelectorOnTop="false" />




When I run my app i get this error

Application Stoppes

and this is what is in android monitor in case that you may need:

Android Monitor Screen

This is what I have done so far.

enter image description here

Answer

Just refer this.

You need to only do this android.R.layout.simple_list_item_1.

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

EDIT 1

What is simple_list_item_1 ?

It is a reference to a built-in XML layout document that is part of the Android OS, rather than one of your own XML layouts.