我一直在尝试了解如何填充ListView并且这样做我尝试制作一个简单的应用程序,它接受一个字符串数组并将其放在listview上。我创建了一个ListView对象'myListView',并尝试为此对象调用'setAdapter()'函数。但'setAdapter'部分变红了,它给我一条错误信息, “无法解析符号'setAdapter'”
以下是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);
}
答案 0 :(得分:5)
请在setContentView之后移除}并将new}放在代码
的末尾@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);
}
}