在的ShowDialog按钮列表视图适配器 [英] showDialog in button Listview adapter
问题描述
我有一个像<一个一个ListView href="http://stackoverflow.com/questions/30092158/set-button-onclick-in-listview-android/30092841#30092841">THIS
我想,当我preSS删除。它显示一个对话框,类似这样的图片
i want when i press the delete. it show a dialog like this image
所以当我preSS YES。它会删除列表。
so when i press YES. it will remove from list.
这是我的code ..
here's my code..
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;
}
}
我该怎么做才能创建一个对话框类似that..i尝试这个code
how can i do to create a dialog like that..i tried this code
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();
但itsnt成功。
but itsnt success.
我得到这个错误
推荐答案
创建一个接口
:
public interface OnDeleteListener {
public void onDelete(String message);
}
当您在初始化 customadapter
发送 OnDeleteListener
作为一个参数:
When you are initializing customadapter
send OnDeleteListener
as a parameter:
private OnDeleteListener mListener;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, OnDeleteListener mListener) {
this.context = context;
this.oslist = oslist;
this.mListener = mListener;
}
然后在删除按钮,点击
检查监听器
是否激活与否:
then on delete button click
check for listener
to whether activate it or not:
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");
}
});
最后在
活动/片段
和监听器调用初始化
适配器code>节目对话框
:
and finally initialize
adapter in your activity/fragment
and on listener invoke
show 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);
}
});
您可以创建的独立的功能,code清除率的并调用它时,你想用
You can create a seperate function for code clearance and call it whenever you want to use
(也注意到,创建一个自定义对话框
你有膨胀
它{也许这就是为什么你得到一个错误})
(also notice that, to create a custom dialog
you have to inflate
it {probably thats why you are getting an error}):
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();
注意:我已经很难codeD这个答案让任何语法错误
可发生
note: I have hardcoded this answer so any syntax error
can occur
这篇关于在的ShowDialog按钮列表视图适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!