我有一个像iframe method
这样的列表视图
我想按下删除时。它会显示一个像这个图像的对话框
所以当我按YES时。它将从列表中删除。
这是我的代码..
'Release Dev Status'!A$1:Z$1000
}
如何创建像这样的对话框...我试过这段代码
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;
}
但它的成功。
我收到了这个错误
答案 0 :(得分:6)
创建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
都可以发生
答案 1 :(得分:1)
尝试此代码。对于Dialog,您必须使用Activity的实例而不是应用程序上下文。
final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);
答案 2 :(得分:0)
首先通过这样的活动。
list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));
然后像这样得到父活动..
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);
答案 3 :(得分:0)
您也可以在活动中执行此操作。对于适配器代码,在按钮上添加一些标签以标识按下了哪个按钮。您还可以将位置设置为标记。
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中为自定义对话框添加此内容。 机器人:的onclick =&#34; deleteMethod&#34;
在以下活动中编写您的方法:
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();
}
答案 4 :(得分:0)
我认为有一种简单的方法可以做到这一点,在我的情况下,我已经遵循了这段代码。
编写一个方法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();
}
用户单击“是”或“否”按钮后,对话框将自动关闭。你无需做任何事情