如何在对话框中打印arraylist

时间:2018-11-20 13:53:31

标签: android-studio arraylist dialog

我正在创建一个应用,该应用在列表视图中列出所有国家/地区,并且用户可以查看这些国家/地区。单击该按钮后,将弹出一个对话框,并列出所选的国家/地区。

我正在尝试将国家/地区添加到arraylist,然后在对话框中打印出array list。但是我无法在ButtonOnClicklistener函数中调用arraylist。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView listView=(ListView)findViewById(R.id.ListView_1);
    CustomAdapter customAdapter = new CustomAdapter();
    listView.setAdapter(customAdapter);
    Button sbmButton = (Button)findViewById(R.id.Btn_submit);
    final ArrayList<String> ArrayCountry = new ArrayList<String>();

    sbmButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Your Wishlist");
            builder.setMessage("Countries");
            builder.setCancelable(false);

            for(int x=0; x < ArrayCountry.size(); x++) {
                builder.setMessage( ArrayCountry.get(x));
            }

            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

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

        }
    });
}

 class CustomAdapter extends BaseAdapter{
    @Override
    public int getCount() {
        return img.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

 @Override
    public View getView(int i, View view, ViewGroup parent) {
        view = getLayoutInflater().inflate(R.layout.customlayout,null);

        ImageView img_athens = (ImageView)view.findViewById(R.id.imageView_City);
        TextView txt_athens = (TextView)view.findViewById(R.id.textView_city);
        TextView txt_greece = (TextView)view.findViewById(R.id.textView_country);
        final CheckBox chk_City = (CheckBox)view.findViewById(R.id.checkBox_Country);
        final ArrayList<String> ArrayCountry = new ArrayList<String>();

        img_athens.setImageResource(img[i]);
        txt_athens.setText(City[i]);
        txt_greece.setText(Country[i]);

        chk_City.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(chk_City.isChecked()){
                        ArrayCountry.add(chk_City.getText().toString());
                    }

            }
        });
        return view;
    }

0 个答案:

没有答案