Android Studio cannot recognize "setAdapter()" method

10.7k views Asked by At

I have been trying to understand how to populate a ListView and to do that I tried making a simple app that takes a string array and puts it on a listview. I created a ListView object 'myListView' and tried calling the 'setAdapter()' function for this object. But the 'setAdapter' part turns red and it gives me an error message saying, "cannot resolve symbol 'setAdapter'"

Following is the source code for MainActivity.java:

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AbsListView;

public class MainActivity extends Activity {

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

String[] myArray = {"biscuits", "laptops", "mice", "headphones", "macBook", "bottles", "something", "also something",
                    "this list is stupid", "and", "dumb", "and", "useless", "okay", "here it ends"};

    ListAdapter myListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, myArray);

    ListView myListView = (ListView) findViewById(R.id.myListView);

    myListView.setAdapter(myListAdapter);
}
1

There are 1 answers

1
Santosh Kathait On BEST ANSWER

Please remove } after setContentView and put new } at the end of code

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


String[] myArray = {"biscuits", "laptops", "mice", "headphones", "macBook", "bottles", "something", "also something",
                    "this list is stupid", "and", "dumb", "and", "useless", "okay", "here it ends"};

    ListAdapter myListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, myArray);

    ListView myListView = (ListView) findViewById(R.id.myListView);

    myListView.setAdapter(myListAdapter);
}
}