Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

android - showDialog in button Listview adapter

i have a listView like THIS

enter image description here

i want when i press the delete. it show a dialog like this image

enter image description here

so when i press YES. it will remove from list.

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;
}

}

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();

but itsnt success.

i got this error

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create an interface:

public interface OnDeleteListener {
    public void onDelete(String message);
}

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");
        }
    });  

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);
}
});

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();

note: I have hardcoded this answer so any syntax error can occur


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...