我想创建一个可点击的列表视图,当按下这些项目时,每个字符串都会打开另一个活动,但我不知道如何在android studio中执行此操作...这里是' s我试过的代码。谢谢。
public class MainActivity extends AppCompatActivity {
private String[] monthsArray = { "JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY",
"AUG", "SEPT", "OCT", "NOV", "DEC" };
private ListView monthsListView;
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
monthsListView = (ListView) findViewById(R.id.months_list);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,monthsArray);
monthsListView.setAdapter(arrayAdapter);
}
private void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
String openClass = classNames[0];
try{
Class selected = Class.forName("com.example.anupambiswas.firststlistview." + openClass);
Intent selectedIntent = new Intent(this,selected);
startActivity(selectedIntent);
}
catch( ClassNotFoundException e){
e.printStackTrace();
}
}}
答案 0 :(得分:1)
在onCreate方法中添加它。
monthsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String openClass = classNames[0];
try{
Class selected = Class.forName("com.example.anupambiswas.firststlistview." + openClass);
Intent selectedIntent = new Intent(this,selected);
startActivity(selectedIntent);
}
catch( ClassNotFoundException e){
e.printStackTrace();
}
}
});