自定义对话框列表视图的问题

时间:2011-02-05 08:49:27

标签: android

我在父活动中调用对话框..对话框在另一个活动中...我在设置适配器时遇到错误“setListAdapter(new ListViewAdapter(this));”...可以任何一个plz辅助我


CustomizeDialog.class

package org.me.dailogfrmchildact;



/** Class Must extends with Dialog */
/** Implement onClickListener to dismiss dialog when OK Button is pressed */
public class CustomizeDialog extends Dialog implements OnClickListener {
    Button okButton;

    public CustomizeDialog(Context context) {
        super(context);
        /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        /** Design the dialog in main.xml file */
        setContentView(R.layout.main);

        okButton = (Button) findViewById(R.id.OkButton);
        okButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        /** When OK Button is clicked, dismiss the dialog */
        if (v == okButton){
            ListviewContent.add("sadas");
        ListviewCount.add("dasasd");
        setListAdapter(new ListViewAdapter(this));

        }

    }
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

package org.me.dailogfrmchildact;


public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        /** Display Custom Dialog */
        CustomizeDialog customizeDialog = new CustomizeDialog(this);
        customizeDialog.show();
// ToDo add your GUI initialization code here        
    }

}

3 个答案:

答案 0 :(得分:1)

您无法在对话框中调用setListAdapter,因为这是Activity的方法。但是你应该做的是在你的布局中创建一个ListView(现在它被称为main)并为它分配一个id:

<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

现在,您可以在代码中找到ListView并为其设置适配器:

ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ListViewAdapter(this));

答案 1 :(得分:0)

据我所知,SetListAdapter是类ListView的一种方法。 因此,您必须扩展listActivity,或者从listview

的对象中调用它

答案 2 :(得分:0)

我解决了问题,这是答案。按照第一个答案创建一个名称照常的列表视图。

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

// access the context again...


final Context cs=context;

//then use this "cs" to set the adapter...


list.setAdapter(new ListViewAdapter(cs));


//...
}