gpt4 book ai didi

android - 如何在android中将自定义对话框作为listview itemClickListener

转载 作者:行者123 更新时间:2023-11-29 17:20:37 26 4
gpt4 key购买 nike

我想在我的 android 项目中使用自定义对话框作为点击 ListView 的项目

我在不同的类中自定义我的对话框如下:

 public class CustomDialogOption extends Dialog {
public Activity activity;
public Button dialog_delete, dialog_edit;
public TextView dialog_msg;

public CustomDialogOption (Activity a) {
super(a);
this.activity = a;
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_counting);

dialog_msg = (TextView) findViewById(R.id.dialogContet);
dialog_delete = (Button) findViewById(R.id.delete);
dialog_edit = (Button) findViewById(R.id.edit);




dialog_msg.setText("..." + user_number.get(i) + " "
+ user_vow.get(i) + " " + user_why.get(i));
dialog_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
getApplicationContext(),
user_number.get(i) + " "
+ " delete..", Toast.LENGTH_LONG).show();


}
});

我想在我的 ListView 上显示这个对话框,点击如下:MainActiviyclass

public class MainActivity extends AppCompatActivity  {
private ListView listView;


public final static ArrayList<String> user_number = new ArrayList<String>();


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


//remain numbers -item click listener for Done-vow
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {


CustomDialogOption cdo = new CustomDialogOption(MainActivity.this ,i);
cdo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdo.show();
}
});

我的问题是如何在 OnItemClickListener 方法 (int i) 和 CustomDialogOption 类之间建立关系。 “i”参数未定义。我能做些什么?感谢您的帮助。

最佳答案

在 CustomDialogOption 中定义一个 int i 或任何你命名的东西。

像这样将 int i 作为参数添加到构造函数中

 public CustomDialogOption (Activity a, Int i) {
super(a);
this.activity = a;
this.i = i;
}

然后这样调用它

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
CustomDialogOption cdo = new CustomDialogOption(MainActivity.this, i);
cdo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdo.show();
}
});

关于android - 如何在android中将自定义对话框作为listview itemClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36686156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com