按钮列表视图适配器中的 showDialog
showDialog in button Listview adapter
我有一个类似
的列表视图
我想按删除键。它显示了一个像这张图片这样的对话框
所以当我按“是”时。它将从列表中删除。
这是我的代码..
public class customadapter extends BaseAdapter{
ArrayList<HashMap<String, String>> oslist;
Context context;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return oslist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return oslist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println("oslist oslist = "+oslist);
System.out.println("oslist 1 = "+oslist);
System.out.println("oslist size = "+oslist.size());
System.out.println("oslist oslist = "+oslist.getClass());
System.out.println("position = "+position);
System.out.println("convertView = "+convertView);
System.out.println("parent = "+parent);
System.out.println("position = "+position);
LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lif.inflate(R.layout.listitem, null);
TextView id = (TextView) convertView.findViewById(R.id.varId);
TextView noNota = (TextView) convertView.findViewById(R.id.varNoNota);
TextView senderName = (TextView) convertView.findViewById(R.id.varSenderName);
TextView totalAmount = (TextView) convertView.findViewById(R.id.varTotalAmount);
id.setText(oslist.get(position).get("id"));
noNota.setText(oslist.get(position).get("noNota"));
senderName.setText(oslist.get(position).get("senderName"));
totalAmount.setText(oslist.get(position).get("totalAmount"));
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Edit ditekan!", Toast.LENGTH_LONG).show();
//I want show YES NO dialog here.
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
}
});
return convertView;
}
}
我怎样才能创建这样的对话框..我试过这段代码
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
但没有成功。
我遇到了这个错误
试试这个code.For一个对话框,你必须使用Activity的实例而不是应用程序上下文。
final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);
首先像这样通过activity。
list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));
然后像这样得到父 activity..
private Activity parentActivity;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
this.parentActivity = parentactivity;
}
并像这样设置对话框生成器..
final Dialog dialog = new Dialog(parentActivity);
创建 interface
:
public interface OnDeleteListener {
public void onDelete(String message);
}
初始化时 customadapter
将 OnDeleteListener
作为参数发送:
private OnDeleteListener mListener;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, OnDeleteListener mListener) {
this.context = context;
this.oslist = oslist;
this.mListener = mListener;
}
然后在 delete button click
检查 listener
是否激活它:
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
if(mListener != null)
mListener.onDelete("The message you want to show");
}
});
最后 initialize
适配器在你的 activity/fragment
和 listener invoke
上显示 Dialog
:
customadaper mAdapter = new customadapter(ArrayList<HashMap<String, String>> oslist,Context context, new OnDeleteListener(){
@Override
public void onDelete(String msg){
//Show your dialog here
//msg - you can send any parameter or none of them through interface just as an example i send a message to show
showDialog(msg);
}
});
您可以创建一个单独的代码清理函数,并在需要时调用它
(另请注意,要创建 custom dialog
,您必须 inflate
它{可能这就是您出现错误的原因}):
private void showDialog(String message){
// set the custom dialog components - text, image and button
inflater = mInflater.inflate(R.layout.your_custom_dialog, null, false);
TextView text = (TextView) inflater.findViewById(R.id.text);
text.setText(message);
ImageView image = (ImageView) inflater.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);
mDialogBuilder.setView(viewFilterDialog);
mDialogBuilder.setCancelable(true);
mDialogBuilder.setTitle(mRes.getString(R.string.dialog_title_filter));
...
final AlertDialog mAlertDialog = mDialogBuilder.create();
mAlertDialog.show();
注意: 我已经硬编码了这个答案,所以任何 syntax error
都可能发生
您也可以在 activity 中执行此操作。对于 Adapter 代码,向您的按钮添加一些标签以识别按下了哪个按钮。您也可以将位置设置为标签。
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setTag(oslist.get(position).get("id"),R.id.varId);
btnDelete .setTag(oslist.get(position).get("id"),R.id.varId);
您可以在 xml 中为您的自定义对话框添加此项。
android:onclick="deleteMethod"
像这样在 activity 中写下你的方法 :
public void deleteMethod(View v)
{
// Get your id or position for which delete button was pressed
String id=v.getTag().toString();
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to delete ?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this, "Deleting...", Toast.LENGTH_SHORT).show();
// You know which item to delete from id / position. delete here.
}})
.setNegativeButton(android.R.string.no, null).show();
}
我认为有一种简单的方法可以做到这一点,在我的例子中,我遵循了这段代码。
编写一个方法 alertMessage()
并在您的按钮侦听器代码中调用它
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
alertMessage();
}
});
public void alertMessage() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE: // Yes button clicked
Toast.makeText(AlertDialogActivity.this, "Yes Clicked",
Toast.LENGTH_LONG).show();
// set your own logic for removal item from listview
break;
case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing
Toast.makeText(AlertDialogActivity.this, "No Clicked",
Toast.LENGTH_LONG).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
对话框将在用户单击 "YES" 或 "NO" 按钮后自行关闭。您无需做任何事情
我有一个类似
我想按删除键。它显示了一个像这张图片这样的对话框
所以当我按“是”时。它将从列表中删除。
这是我的代码..
public class customadapter extends BaseAdapter{
ArrayList<HashMap<String, String>> oslist;
Context context;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return oslist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return oslist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println("oslist oslist = "+oslist);
System.out.println("oslist 1 = "+oslist);
System.out.println("oslist size = "+oslist.size());
System.out.println("oslist oslist = "+oslist.getClass());
System.out.println("position = "+position);
System.out.println("convertView = "+convertView);
System.out.println("parent = "+parent);
System.out.println("position = "+position);
LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lif.inflate(R.layout.listitem, null);
TextView id = (TextView) convertView.findViewById(R.id.varId);
TextView noNota = (TextView) convertView.findViewById(R.id.varNoNota);
TextView senderName = (TextView) convertView.findViewById(R.id.varSenderName);
TextView totalAmount = (TextView) convertView.findViewById(R.id.varTotalAmount);
id.setText(oslist.get(position).get("id"));
noNota.setText(oslist.get(position).get("noNota"));
senderName.setText(oslist.get(position).get("senderName"));
totalAmount.setText(oslist.get(position).get("totalAmount"));
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Edit ditekan!", Toast.LENGTH_LONG).show();
//I want show YES NO dialog here.
}
});
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
}
});
return convertView;
}
}
我怎样才能创建这样的对话框..我试过这段代码
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
但没有成功。
我遇到了这个错误
试试这个code.For一个对话框,你必须使用Activity的实例而不是应用程序上下文。
final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);
首先像这样通过activity。
list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));
然后像这样得到父 activity..
private Activity parentActivity;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
this.parentActivity = parentactivity;
}
并像这样设置对话框生成器..
final Dialog dialog = new Dialog(parentActivity);
创建 interface
:
public interface OnDeleteListener {
public void onDelete(String message);
}
初始化时 customadapter
将 OnDeleteListener
作为参数发送:
private OnDeleteListener mListener;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, OnDeleteListener mListener) {
this.context = context;
this.oslist = oslist;
this.mListener = mListener;
}
然后在 delete button click
检查 listener
是否激活它:
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
if(mListener != null)
mListener.onDelete("The message you want to show");
}
});
最后 initialize
适配器在你的 activity/fragment
和 listener invoke
上显示 Dialog
:
customadaper mAdapter = new customadapter(ArrayList<HashMap<String, String>> oslist,Context context, new OnDeleteListener(){
@Override
public void onDelete(String msg){
//Show your dialog here
//msg - you can send any parameter or none of them through interface just as an example i send a message to show
showDialog(msg);
}
});
您可以创建一个单独的代码清理函数,并在需要时调用它
(另请注意,要创建 custom dialog
,您必须 inflate
它{可能这就是您出现错误的原因}):
private void showDialog(String message){
// set the custom dialog components - text, image and button
inflater = mInflater.inflate(R.layout.your_custom_dialog, null, false);
TextView text = (TextView) inflater.findViewById(R.id.text);
text.setText(message);
ImageView image = (ImageView) inflater.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);
mDialogBuilder.setView(viewFilterDialog);
mDialogBuilder.setCancelable(true);
mDialogBuilder.setTitle(mRes.getString(R.string.dialog_title_filter));
...
final AlertDialog mAlertDialog = mDialogBuilder.create();
mAlertDialog.show();
注意: 我已经硬编码了这个答案,所以任何 syntax error
都可能发生
您也可以在 activity 中执行此操作。对于 Adapter 代码,向您的按钮添加一些标签以识别按下了哪个按钮。您也可以将位置设置为标签。
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setTag(oslist.get(position).get("id"),R.id.varId);
btnDelete .setTag(oslist.get(position).get("id"),R.id.varId);
您可以在 xml 中为您的自定义对话框添加此项。 android:onclick="deleteMethod"
像这样在 activity 中写下你的方法 :
public void deleteMethod(View v)
{
// Get your id or position for which delete button was pressed
String id=v.getTag().toString();
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to delete ?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this, "Deleting...", Toast.LENGTH_SHORT).show();
// You know which item to delete from id / position. delete here.
}})
.setNegativeButton(android.R.string.no, null).show();
}
我认为有一种简单的方法可以做到这一点,在我的例子中,我遵循了这段代码。
编写一个方法 alertMessage()
并在您的按钮侦听器代码中调用它
btnDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//I want show YES NO dialog here
alertMessage();
}
});
public void alertMessage() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE: // Yes button clicked
Toast.makeText(AlertDialogActivity.this, "Yes Clicked",
Toast.LENGTH_LONG).show();
// set your own logic for removal item from listview
break;
case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing
Toast.makeText(AlertDialogActivity.this, "No Clicked",
Toast.LENGTH_LONG).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
对话框将在用户单击 "YES" 或 "NO" 按钮后自行关闭。您无需做任何事情