Android 如何在对话框中设置自定义列表视图

Android 如何在对话框中设置自定义列表视图,android,listview,dialog,Android,Listview,Dialog,我正在开发一个应用程序,它从web服务获取一些数据并显示在列表视图中。我实现了一个自定义适配器,它由BaseAdapter扩展。在getView()方法中,我还对原始数据进行了充气。。这些都很好用 我的问题是,我已经实现了当用户单击列表项时显示对话框的代码,但现在我想显示另一个对话框,其中包含自定义列表(当单击“是”按钮时)。我还想在列表视图中显示一些数据。[我有一个装满我想要的数据的ArrayList]。我正在适配器类中编写代码。有人能告诉我怎么做吗 这是我的代码: public class

我正在开发一个应用程序,它从web服务获取一些数据并显示在列表视图中。我实现了一个自定义适配器,它由BaseAdapter扩展。在
getView()
方法中,我还对原始数据进行了充气。。这些都很好用

我的问题是,我已经实现了当用户单击列表项时显示对话框的代码,但现在我想显示另一个对话框,其中包含自定义列表(当单击“是”按钮时)。我还想在列表视图中显示一些数据。[我有一个装满我想要的数据的ArrayList]。我正在适配器类中编写代码。有人能告诉我怎么做吗

这是我的代码:

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) {
        super();
        this.resource=resource;
        this.data = d;
        this.activity = act;
        this.mContext = ctx;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }


    public void dialogshow(final String Date,final String Start,final String End){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
        alertDialogBuilder.setTitle("Confirm your Action!");

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

                    //check similer records

                //  ShortList sh = new ShortList();

                //  ArrayList<HashMap<String, String>> duplicateList; 
                //  duplicateList=sh.getDuplicated(Date, Start, End);


                    //if duplicates > 1 then show the popup list
            //      if(duplicateList.size()>1){
                    AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);

                    LayoutInflater infl = activity.getLayoutInflater();




                    //View vi = infl.inflate(id, root)





                    alertDialogBuilder2.setView(infl.inflate(R.layout.dialog_row, null))
                    .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

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

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

                    alertDialogBuilder2.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();
                    }
                });
        alertDialogBuilder.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);


        final TextView firstname = (TextView) vi.findViewById(R.id.fname);
        final TextView lastname = (TextView) vi.findViewById(R.id.lname);
        final TextView startTime = (TextView) vi.findViewById(R.id.stime);
        final TextView endTime = (TextView) vi.findViewById(R.id.etime);
        final TextView date = (TextView) vi.findViewById(R.id.blank);
        final 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);
                /*Intent zoom=new Intent(mContext, Profile.class);
                zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(zoom);*/

                // get the intent from the hashmap check if there is similar date and time.
                //then store them in a list or array.

                String getDate = (String) date.getText();
                String getStartTime = startTime.getText().toString();
                String getEndTime = endTime.getText().toString();

                dialogshow(getDate,getStartTime,getEndTime);
            }
    });


        vi.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String getFname = firstname.getText().toString();
                Toast.makeText(parent.getContext(), "view clicked: "+getFname , Toast.LENGTH_SHORT).show();

                //get the id of the view
                //check the id of the request
                //call the web service acording to the id

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



            }
        });

        return vi;


}
公共类NewsRowAdapter扩展了BaseAdapter{
私有上下文;
私人活动;
专用静态充气机=空;
私有数组列表数据;
智力资源;
//字符串响应;
//语境;
//初始化适配器
公共NewsRowAdapter(上下文ctx、活动act、int资源、ArrayList d){
超级();
这个。资源=资源;
这个数据=d;
这个活动=行动;
this.mContext=ctx;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
公共无效对话框显示(最终字符串日期、最终字符串开始、最终字符串结束){
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(活动);
setTitle(“确认您的操作!”);
//设置对话框消息
alertDialogBuilder
.setMessage(“单击是退出!”)
.setCancelable(错误)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
//如果单击此按钮,请关闭
//当前活动
//MainActivity.this.finish();
//Toast.makeText(mContext,“单击是”,Toast.LENGTH_LONG.show();
//检查相似记录
//短名单sh=新短名单();
//ArrayList重复列表;
//duplicateList=sh.getDuplicated(日期、开始、结束);
//如果重复项>1,则显示弹出列表
//如果(duplicateList.size()>1){
AlertDialog.Builder alertDialogBuilder2=新建AlertDialog.Builder(活动);
LayoutInflater infl=activity.getLayoutInflater();
//视图vi=充气(id,根部)
alertDialogBuilder2.setView(充气(R.layout.dialog_行,空))
.setPositiveButton(“接受”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
Toast.makeText(mContext,“接受”,Toast.LENGTH_LONG.show();
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
//TODO自动生成的方法存根
dialog.dismise();
}
});
alertDialogBuilder2.show();
//  }
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
//如果单击此按钮,只需关闭
//打开对话框,不执行任何操作
dialog.cancel();
}
});
alertDialogBuilder.show();
}
@凌驾
公共视图getView(最终整型位置、视图转换视图、最终视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(右侧布局行,空);
final TextView firstname=(TextView)vi.findviewbyd(R.id.fname);
最终TextView lastname=(TextView)vi.findViewById(R.id.lname);
最终文本视图开始时间=(文本视图)vi.findViewById(R.id.stime);
最终文本视图结束时间=(文本视图)vi.findViewById(R.id.etime);
最终文本视图日期=(文本视图)vi.findViewById(R.id.blank);
最终图像视图img=(图像视图)vi.findviewbyd(R.id.list_图像);
HashMap宋=新HashMap();
宋=数据。获取(位置);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_date));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY\u THUMB\u URL),img);
按钮接受=(按钮)vi.findViewById(R.id.button1);
accept.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
最终整数x=(整数)getItemId(位置);
/*意图缩放=新意图(mContext,Profile.class);
zoom.setFlags(意图、标志、活动、多任务);
zoom.setFlags(意图、标志、活动、新任务);
mContext.startActivity(缩放)*/
//通过hashmap检查是否有类似的日期和时间来获取意图。
//然后将它们存储在列表或数组中。
字符串getDate=(字符串)date.getText();
字符串getStartTime=startTime.getText().toString();
 final Dialog new_dialog = new Dialog(getParent());
 new_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 new_dialog.setContentView(R.layout.customize_dialog_list_view);
 new_dialog.setCancelable(false);
 cuc = new CommanUtilityClass();
 SharedPreferences sp = getSharedPreferences("provider",0);
   String services = sp.getString("services","");
   Log.i("Servicesss",bAllServices);

   TextView service = (TextView) new_dialog.findViewById(R.id.cdlv_service_provider);
   TextView hour = (TextView) new_dialog.findViewById(R.id.cdlv_working_hours);

   TextView appointment_time = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_time);
   TextView appointment_date = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_date);

   String[] ampm = myTiming[which].split(":");

   Log.d("xxxooo", ampm[0]);

   appointment_time.setText(Html.fromHtml("<b>Appointment time :</b>" + myTimingToShow[which].split("/")[0]));
   appointment_date.setText(Html.fromHtml("<b>Appointment date :</b>" + selected));

   service.setText(Html.fromHtml("<b>Service provider :</b>"+ cuc.toTheUpperCase(bsp_name)));

   hour.setText(Html.fromHtml("<b>Working hours :</b>"+ cuc.toTheUpperCase(bsp_availability)));

   lv = (ListView) new_dialog.findViewById(R.id.cdlv_list);

   CustomDialogArrayAdapter cdaa = new CustomDialogArrayAdapter(getApplicationContext(),m_ArrayList);

   lv.setAdapter(cdaa);

   new_dialog.show();

   ImageButton btn_cdlv_cancel = (ImageButton) new_dialog.findViewById(R.id.cdlv_cancel);

   btn_cdlv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new_dialog.dismiss();
            }
        });

   lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(
            AdapterView<?> arg0,
            View arg1,
            int arg2,
            long arg3) {

        Log.d("checkforbookedslots", booked_slots);

        final_time_selected = myTiming[which];
        String final_duration = m_ArrayList.get(arg2).provider_service_duration;
        Log.d("adiadicheck", check_for_booking_list);
        if (checkOverlapSlot(final_time_selected,final_duration)) {
            Log.d("gp","seven12aa");
            SharedPreferences sp = getSharedPreferences("booking_detail",0);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString("provider_service",m_ArrayList.get(arg2).provider_service);
            editor.putString("provider_service_duration",m_ArrayList.get(arg2).provider_service_duration);
            editor.putString("provider_service_price",m_ArrayList.get(arg2).provider_service_price);
            editor.putString("service_id",m_ArrayList.get(arg2).provider_service_id);
            editor.commit();

            SessionManagement sm = new SessionManagement(getParent());
            // sm.checkLogin();

            if (sm.isLoggedIn()) {
                Log.d("viv2","1");
                Intent edit = new Intent(SelectedService.this,UserLoggedActivity.class);
                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.startChildActivity("UserLoggedActivity",edit);
            } else {
                Log.d("viv2","2");
                Intent edit = new Intent(SelectedService.this,UserNoLoggedActivity.class);
                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.startChildActivity("UserNoLoggedActivity",edit);
            }
            new_dialog.dismiss();
        }
    }
   });
    //String[] list_data; Preloaded with a String array

    final CharSequence[] items = new CharSequence[list_data.length];

            for (int i = 0; i < list_data.length; i++) {
               items[i] = list_data[i];
            }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select the data you want");
    builder.setItems(items, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            //Get the id of the item
            diag_callback();

        }

    });

    AlertDialog alert = builder.create();
    alert.show();

}

public void diag_callback() {
    //Do someting when the user has made his selection
}
private Button btnOpenDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ArrayList<String> strArrlst = new ArrayList<String>();
    for (int i = 0; i < 15; i++) {
        strArrlst.add("Number: " + i);
    }

    btnOpenDialog = (Button) findViewById(R.id.btn_open_dialog);
    btnOpenDialog.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.d("Dialog box is btn click", "");
            final Dialog dialog = new Dialog(MainActivity.this);
            Toast.makeText(getApplicationContext(), "btn is click", Toast.LENGTH_SHORT).show();
            // tell the Dialog to use the dialog.xml as it's layout
            // description
            dialog.setContentView(R.layout.custom_dailog_box);
            dialog.setTitle("Android Custom Dialog Box");
            TextView txt = (TextView) dialog.findViewById(R.id.txt);
            txt.setText("This is an Android custom Dialog Box Example! Enjoy!");

            ListView dialogLIst = (ListView) dialog.findViewById(R.id.lstvw_open_custom_dialog);
            // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            // android.R.layout.simple_list_item_1, android.R.id.text1,
            // values);
            // dialogLIst.setAdapter(adapter);
            AdapterListC adapListC = new AdapterListC(getApplicationContext(), strArrlst);
            dialogLIst.setAdapter(adapListC);

            dialogLIst.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                    Toast.makeText(getApplicationContext(), "This is click position is" + position, Toast.LENGTH_SHORT).show();
                }

            });
            dialog.show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public AdapterListC(Context context_, ArrayList<String> arrlstString) {
    super();
    this.context_ = context_;
    this.arrlstString = arrlstString;
    mInflater = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

Context context_;
ArrayList<String> arrlstString;
private LayoutInflater mInflater;

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

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return arrlstString.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Viewolder holder = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem, null);
        holder = new Viewolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.txt_listitem);

        convertView.setTag(holder);
    } else {
        holder = (Viewolder) convertView.getTag();
    }
    holder.txtName.setText(arrlstString.get(position).toString());

    return convertView;
}

class Viewolder {
    TextView txtName;
}