0

你好,

我正在尝试执行以下步骤:
1)在 ListView 上选择项目(歌曲标题)
2)一旦选择项目 Alertdialog 已启动(带有“Listen”和“Download”列表)
3)在 Alertdialog 上选择两个项目之一
4)创建Intent with extra to start Activity(下载文件)

不明白如何实施步骤 3 和 4。

这是 ItemClickListener 的部分代码:

ls.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.d(null, ". Position: " + position );
        showDialog(position);
    }
    });

并与创建对话框分开

protected Dialog onCreateDialog(int id){

AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
final String[] service = {"Play music", "Download"};
adb.setTitle("Choose service");
    switch(id){
    case 0:
            Log.d(null, service[id] + ". Position: " );
            adb.setItems(service, myClickListener);
            break;
    case 1:
            Log.d(null, service[id] + ". Position: " );
            adb.setItems(service, myClickListener);

    break;
}

return adb.create();
}

它适用于 2 个项目(歌曲),但我想在超过 2 个项目的情况下更改代码。

4

1 回答 1

0

回答我的问题)

这是对我有用的代码:

ls.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final int selectedPosition = position;
                final String[] service = {"Play music", "Download"};
                AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
                adb.setTitle("Choose service");
                adb.setItems(service, new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch(which){
                        case 0:
                            Intent playIntent = new Intent(getApplicationContext(), PlayMusicActivity.class);
                            playIntent.putExtra("songIndex", selectedPosition);
                            Log.d(null, which + ". Position: " + selectedPosition );
                            break;
                        case 1:
                            Intent downloadIntent = new Intent(getApplicationContext(), DownloadActivity.class);
                            downloadIntent.putExtra("songIndex", selectedPosition);
                            Log.d(null, which + ". Position: " + selectedPosition );
                            break;
                        }

                    }
                });
                adb.show();
            }
            });

于 2013-09-24T20:18:01.827 回答