将数据从customdialog类传输到父类

时间:2011-02-08 06:33:34

标签: java android

这里我通过我的父活动调用customdialog ..其中,对话框在另一个活动中包含listview ...从该对话框我从listview传输选定的内容..但未能实现我的目标..并尝试了许多方式......任何人都可以帮助我实现它。 注意:我在listitemclick监听器上遇到错误......

customdialog.class

public class CustomizeDialog extends Dialog {
org.me.dailogfrmchildact.MainActivity ma;
    Button cancelButton;
    ListView list;
    int item_pos;

    public CustomizeDialog(Context context) {
        super(context);
         Context cs = context;

        /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog);
        ma=new MainActivity();
        list = (ListView) findViewById(R.id.list);
        cancelButton = (Button) findViewById(R.id.OkButton);
        ListviewContent.add("item1");
        ListviewCount.add("20");
        ListviewContent.add("item2");
        ListviewCount.add("30");
        list.setAdapter(new ListViewAdapter(cs));

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {    
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                item_pos = pos;
                System.out.println("===========item_pos" + pos);
                ma.txt.setText(ListviewContent.get(pos).toString());    
                dismiss();                    
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                dismiss();
            }
        });
    }    

    private static ArrayList<String> ListviewContent = new ArrayList<String>();
    private static ArrayList<String> ListviewCount = new ArrayList<String>();

    private static class ListViewAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ListViewAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return ListviewContent.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public String getCount(int position) {
            return ListviewCount.get(position);
        }

        public String[] getSizeType(int position) {
            String[] str = new String[2];
            str[0] = ListviewContent.get(position);
            str[1] = ListviewCount.get(position);
            return str;
        }

        public long getItemId(int position) {
            return position;
        }

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

            ListContent holder;    
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listviewinflate, null);
                holder = new ListContent();
                holder.text = (TextView) convertView.findViewById(R.id.TextView01);
                holder.text.setCompoundDrawables(null, null, null, null);
                holder.count = (TextView) convertView.findViewById(R.id.TextView02);
                holder.count.setCompoundDrawables(null, null, null, null);
                convertView.setTag(holder);
            } else {    
                holder = (ListContent) convertView.getTag();
            }    
            holder.text.setText(ListviewContent.get(position));
            holder.count.setText(ListviewCount.get(position));
            return convertView;
        }    
        static class ListContent {
            TextView text;
            TextView count;
        }
    }
}

mainactivity.class:

public class MainActivity extends Activity {
    org.me.dailogfrmchildact.CustomizeDialog cd;
    Button btn;
    EditText txt;
    CustomizeDialog customizeDialog;
    boolean click = false;    

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        cd = new CustomizeDialog(this);
        btn = (Button) findViewById(R.id.btn1);
        txt = (EditText) findViewById(R.id.txt1);
        /** Display Custom Dialog */
        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                customizeDialog = new CustomizeDialog(MainActivity.this);
                customizeDialog.show();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

问题是当你应该首先使用创建CustomizeDialog的那个时,你正在CustomizeDialog中创建一个新的MainActivity。

让CustomizeDialog接受MainActivity而不是Context,并将ma设置为等于。

编辑: 将CustomizeDialog的构造函数更改为:

public CustomizeDialog(MainActivity mainActivity) {
   super(mainActivity);
   ma = mainActivity; // Instead of ma = new MainActivity()

   // everything else is the same
}