button.setId(整数值)给我查看错误

时间:2010-11-27 13:23:17

标签: android android-layout

大家好我已经实现了我的自定义simpleCursorAdapter并将其视图与另一个视图合并以创建我想要的页面,但是当我尝试将id设置为列表视图记录的每个按钮时,它会给我一个视图错误

一半的逻辑来自这个链接:

http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/

以及我的自定义视图的其他代码部分如下:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.fr1);


        Cursor c = getContentResolver().query(People.CONTENT_URI,
                null, null, null, null);
                startManagingCursor(c);
                String[] cols = new String[]{People.NAME};
                int[] names = new int[]{R.id.FriendsName};

        SimpleCursorAdapter rows = new MyAdapter(this,R.layout.flistview,c,cols,names);
        setListAdapter(rows);           
    }

public class MyAdapter extends SimpleCursorAdapter{

        public DisplayFriendsAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View view = super.getView(position, convertView, parent);
            ImageButton Btn1 = (ImageButton)view.findViewById(R.id.Button1);
            Btn1.setId(position);
            ImageButton Btn2 = (ImageButton)view.findViewById(R.id.Button2);
            Btn2.setId(position);
            Btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.i("Accepted", "clicked"+v.getId());

                }
            }); 

            Btn2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {


                }
            }); 
            return view;
        }
    }

1 个答案:

答案 0 :(得分:0)

无需设置按钮的ID。它的ID已在用户界面的XML中定义,并在R.java中作为常量放置为R.id.Button1。只需删除.setID(...)行代码即可。