Bowiz2
Bowiz2

Reputation: 39

listView.setAdapter doesn't work

I have a listview called quotesList, and I am trying to use an adapter to put information in it. Here is my code:

ListView listView = (ListView) findViewById(R.id.quotesList);
String[]values={"Android","iOS","Windows Phone","Other Stuff"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,values);
listView.setAdapter(adapter);

The only error that Eclipse shows is in the listView.setAdapter(adapter) line. The bold represents where the red squiggly is.

Syntax error on token "adapter", VariableDeclaratorId expected after this token

The period after listView gives an error as well, but I'm pretty sure its just related to the other error, as its a syntax error. Syntax error on token(s), misplaced construct(s) Thanks in advance!

Upvotes: 4

Views: 9200

Answers (3)

anoop
anoop

Reputation: 11

I faced the same problem .For me the reason is , declaring the ListView outside the onCreate() method.

Upvotes: 0

Jan Koester
Jan Koester

Reputation: 1218

Try to clean up your project. Your code is correct. Select Project -> Clean in if you using eclipse as Android Development IDE. This should fix this.

EDIT: For me this code works in my Eclipse IDE:

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.quotesList);
        String[]values={"Android","iOS","Windows Phone","Other Stuff"};
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,values);
        listView.setAdapter(adapter);
    }
  }

Upvotes: 3

shalinshah1993
shalinshah1993

Reputation: 49

Remove the third parameter form your ArrayAdapter initialization ie ArrayAdapter<String> ad = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);

Upvotes: 0

Related Questions