单击自定义列表视图项中的对话框

时间:2013-11-27 09:58:08

标签: android listview dialog

我正在开发一个应用程序,它从Web服务获取数据并将其显示在列表视图中。我正在使用自定义列表视图适配器。在列表的一行中有两个按钮。我想在点击一个按钮时显示一个对话框。当按下一个对话框按钮时我想对它做一些动作。

这是我的Adapter类:

public class NewsRowAdapter extends BaseAdapter  {

private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
    //String response;
    //Context context;
    //Initialize adapter
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d, DialogCreatorInterface di) {
        super();
        this.resource=resource;
        this.data = d;
        this.activity = act;
        this.mContext = ctx;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }



    public interface DialogCreatorInterface{
        public void showDialog();
    }


    DialogCreatorInterface dialogCreatorInterface  = new DialogCreatorInterface() {

        @Override
        public void showDialog() {
            //Create and show the dialog code
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        //MainActivity.this.finish();

                        Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
                    }
                  })
                  .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }
    };

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);

        TextView firstname = (TextView) vi.findViewById(R.id.fname);
        TextView lastname = (TextView) vi.findViewById(R.id.lname);
        TextView startTime = (TextView) vi.findViewById(R.id.stime);
        TextView endTime = (TextView) vi.findViewById(R.id.etime);
        TextView date = (TextView) vi.findViewById(R.id.blank);
        ImageView img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);

        Button accept = (Button) vi.findViewById(R.id.button1);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                //Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();


                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/


                 //dialogCreatorInterface.showDialog();





            }
        });

        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();



                Intent zoom=new Intent(parent.getContext(), Profile.class);   
                parent.getContext().startActivity(zoom);


            }
        });

        return vi;


}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}



@Override
public Object getItem(int possision) {
    // TODO Auto-generated method stub
    return possision;
}



@Override
public long getItemId(int possision) {
    // TODO Auto-generated method stub
    return possision;
}
}

我尝试过一个带有对话界面的东西..但是我无法处理它......请有人帮助我......

修改

这是我目前的Getview方法

public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);

        TextView firstname = (TextView) vi.findViewById(R.id.fname);
        TextView lastname = (TextView) vi.findViewById(R.id.lname);
        TextView startTime = (TextView) vi.findViewById(R.id.stime);
        TextView endTime = (TextView) vi.findViewById(R.id.etime);
        TextView date = (TextView) vi.findViewById(R.id.blank);
        ImageView img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);

        Button accept = (Button) vi.findViewById(R.id.button1);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                //Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();


                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);

                alertDialogBuilder.setTitle("Your Title");
                 alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("YES", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                             Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton("NO", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            dialog.cancel();
                        }
                    });

                 alertDialogBuilder.show(); 

            }
        });

        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();



                Intent zoom=new Intent(parent.getContext(), Profile.class);   
                parent.getContext().startActivity(zoom);


            }
        });

        return vi;


}

1 个答案:

答案 0 :(得分:0)

试试这个..

public class NewsRowAdapter extends BaseAdapter  {

private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
    //String response;
    //Context context;
    //Initialize adapter
    public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d, DialogCreatorInterface di) {
        super();
        this.resource=resource;
        this.data = d;
        this.activity = act;
        this.mContext = ctx;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }        

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {


    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.row,null);

        TextView firstname = (TextView) vi.findViewById(R.id.fname);
        TextView lastname = (TextView) vi.findViewById(R.id.lname);
        TextView startTime = (TextView) vi.findViewById(R.id.stime);
        TextView endTime = (TextView) vi.findViewById(R.id.etime);
        TextView date = (TextView) vi.findViewById(R.id.blank);
        ImageView img = (ImageView) vi.findViewById(R.id.list_image);


        HashMap<String, String> song = new HashMap<String, String>();
        song =data.get(position);

        firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
        lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
        startTime.setText(song.get(MainActivity.TAG_STIME));
        endTime.setText(song.get(MainActivity.TAG_ETIME));
        date.setText(song.get(MainActivity.TAG_DATE));
        //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);

        Button accept = (Button) vi.findViewById(R.id.button1);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final int x = (int) getItemId(position);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,int id) {
                Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();


                })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    @SuppressLint("NewApi")
                    public void onClick(DialogInterface dialog,int id) {

                        dialog.cancel();

                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();
                // show it
                alertDialog.show();    
            }
        });

        return vi;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}



@Override
public Object getItem(int possision) {
    // TODO Auto-generated method stub
    return possision;
}



@Override
public long getItemId(int possision) {
    // TODO Auto-generated method stub
    return possision;
}
}

和你的startActivity(zoom); Intent内部id listview中的wright点击如下

listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long id) {
                Intent zoom=new Intent(Activity.this, Profile.class);   
                startActivity(zoom);
            }
        });