Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-仅指列表点击onDialogPositiveClick?_Android_Adt - Fatal编程技术网

Android 安卓-仅指列表点击onDialogPositiveClick?

Android 安卓-仅指列表点击onDialogPositiveClick?,android,adt,Android,Adt,是否可以在onDialogPositiveClick中引用onListItemClick?我真的可以在onDialogPositiveClick中单击onListItemClick,还是我必须做一些完全不同的事情?这是我的代码…谢谢你的帮助 public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener { @Override protected void onCreate(Bun

是否可以在onDialogPositiveClick中引用onListItemClick?我真的可以在onDialogPositiveClick中单击onListItemClick,还是我必须做一些完全不同的事情?这是我的代码…谢谢你的帮助

public class MainActivity extends ListActivity implements TheDialog.NoticeDialogListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    String[] sites = {"Google", "Amazon", "Ebay" , "Reddit", "SmashingMag", "CCC"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylist_item, R.id.textView1, sites);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    DialogFragment newFragment = new TheDialog();
    newFragment.show(getFragmentManager(), "Confirm");
    Intent i = null;
    switch(position){
    case 0:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        startActivity(i); break;
    case 1:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
        startActivity(i); break;
    case 2:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ebay.com"));
        startActivity(i); break;
    case 3:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.reddit.com"));
        startActivity(i); break;
    case 4:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.smashingmag.com"));
        startActivity(i); break;
    case 5:
        i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.myccc.corning-cc.edu"));
        startActivity(i); break;
    }
}

@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;
}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    // TODO Auto-generated method stub
}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {
    // TODO Auto-generated method stub

}

}
public类main活动扩展列表活动实现dialog.noticediaglistener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[]sites={“Google”、“Amazon”、“Ebay”、“Reddit”、“SmashingMag”、“CCC”};
ArrayAdapter=新的ArrayAdapter(此,R.layout.mylist_项,R.id.textView1,站点);
setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
super.onListItemClick(左、右、位置、id);
DialogFragment newFragment=新建对话框();
show(getFragmentManager(),“Confirm”);
意图i=无效;
开关(位置){
案例0:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.google.com"));
星触觉(i);断裂;
案例1:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.amazon.com"));
星触觉(i);断裂;
案例2:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.ebay.com"));
星触觉(i);断裂;
案例3:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.reddit.com"));
星触觉(i);断裂;
案例4:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.smashingmag.com"));
星触觉(i);断裂;
案例5:
i=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.myccc.corning-cc.edu"));
星触觉(i);断裂;
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共无效onDialogPositiveClick(DialogFragment对话框){
//TODO自动生成的方法存根
}
@凌驾
公共无效onDialogNegativeClick(DialogFragment对话框){
//TODO自动生成的方法存根
}
}

您的对话框
只有在
仅显示项单击完成并根据您的
开关
逻辑处理选择后,对话框才会显示

因此,在对话框被取消后,您不需要再次调用它。我通常建议不要直接调用
onListItemClick

此外,当您在
onListItemClick
中以
Intent.ACTION\u VIEW
启动活动时,您的对话框可能会被浏览器窗口隐藏,直到您返回活动

我猜你们只是希望你们的对话框在你们打开基于点击列表项的Url之前起到确认的作用。您可以通过对代码进行简单更改来实现这一点:

  • 在活动中声明成员意图:
    intent mActionViewIntent

  • 将开关箱更改为:


  • 然后,如果用户通过对话框确认,则使用
    mActionViewIntent
    启动活动:


谢谢你的回复!!您是否有任何关于如何使onListItemClick在onDialogPositiveClick完成之前不会开始的提示?@webhoodlum因为您在
onListItemClick
中创建了对话框,所以在创建对话框之前,它将始终完成,由于
onListItem单击
onDialogPositiveClick
在同一UI线程上运行。我想你想要一些不同的东西:在
onListItem中单击
你用特定的URI创建意图,然后将其传递给你的对话框,当对话框用“OK”按钮完成时,它将打开从意图传递给它的URL(dialog)。如果你使它成为成员变量,你甚至不需要将意图传递给对话框。我在答案中添加了代码,希望能有所帮助
case 0:
    mActionViewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    break; // note not sending intent here
case 1:
    mActionViewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amazon.com"));
    break; 

// ..etc
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    startActivity(mActionViewIntent); // start web-browser
}