代码:
—
图中ArrayAdapter(Context context…)的context在onCreate(Bundle savedInstanceState)里面用SelectionActivity.this还管用,一到自己写的方法里面就报错了。
应该怎么写呢?
代码:
图中ArrayAdapter(Context context…)的context在onCreate(Bundle savedInstanceState)里面用SelectionActivity.this还管用,一到自己写的方法里面就报错了。
应该怎么写呢?
1.最普通的添加文本;
activity中的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ListView listView = (ListView) this.findViewById(R.id.listview2);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
listView.setAdapter(adapter);
}
2,arraylistadapter实现的单选按钮
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice,//单选模式
new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//单选模式
listView.setAdapter(adapter);
3.arrayadapter实现的多选按钮
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,//多选
new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//多选
listView.setAdapter(adapter);