public class MainActivity extends Activity {
static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.activity_main, FRUITS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
以下錯誤出現:方法的onCreate()未找到
公共類MainActivity擴展活動{
既然你:
Gradle: cannot find symbol method
setListAdapter(android.widget.ArrayAdapter<java.lang.String>)
Gradle: cannot find symbol method getListView()
錯誤不幸的是顯示allplication停止 – sagarlakhani62
@ sagarlakhani62:然後你有更多的問題。 activity_main.xml中的ListView是否有android:id =「@ id/android:list」? –
我嘗試這個代碼的第一種方法本頁http://www.mkyong.com/android/android-listview-example/ – sagarlakhani62