当我点击"高级"时,如何打开我的SettingsActivty?我的列表视图对话框中的部分?这是我的代码
public void show (){
ListView listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.dialog_list, new String[]{"Themes", "Advanced", "Help"}));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// Do your stuff here.
}
});
Dialog dialog = new Dialog(this);
dialog.setTitle("Settings");
dialog.setContentView(listView);
dialog.show();
}
我知道它会是这样的,但我怎样才能适应特定的论点
Intent iinent= new Intent(Homeactivity.this,secondactivity.class);
startActivity(iinent);
答案 0 :(得分:1)
试试这段代码:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
switch(arg2){
case(1):
Intent iinent= new Intent(Homeactivity.this,SettingsActivity.class);
startActivity(iinent);
}
}
为了进一步说明,arg0是你的适配器,arg1是你点击的视图,arg2是位置,arg3是id。
编辑:进一步澄清,因为高级是第二个字符串,列表位置有一个基于零的索引(它们从0开始),它将位于位置编号1;
我们还有很多其他方法可以解决这个问题:
通过适配器(可能是您最好的选择)。
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String item = arg0.getAdapter().getItem(arg1);
if(item.equals("Advanced"){
Intent intent = Intent(Homeactivity.this,SettingsActivity.class);
startActivity(intent);
}
}
通过视图:
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView text = (TextView) arg1;
if(text.getText().toString().equals("Advanced"){
Intent intent = Intent(Homeactivity.this,SettingsActivity.class);
startActivity(intent);
}
}
答案 1 :(得分:0)
使用此选项可在第二个位置设置的高级项目上调用活动:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position==1){
Intent iinent= new Intent(Homeactivity.this,secondactivity.class);
startActivity(iinent);
}
}
});