Why can't I click a Button in a ListView using SimpleAdapter in android?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 18:06:44

activity_main.xml does not have Button with id mytask. So you cannot initialize Button in MainActivtiy.

The Button belongs to mytask.xml override getView and have the button listener there.

http://developer.android.com/reference/android/widget/SimpleAdapter.html

Edit:

   SimpleAdapter k=new SimpleAdapter(this,val1,R.layout.mytask,new String[]{"a","c","b"},new int[]{R.id.View1,R.id.View2,R.id.ViewStatus})
    {
        @Override
        public View getView (int position, View convertView, ViewGroup parent)
        {
            View v = super.getView(position, convertView, parent);

             Button b=(Button)v.findViewById(R.id.mytask);
             b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MainActivity.this,"save",Toast.LENGTH_SHORT).show();
                }
            });
            return v;
        }


    };

Edit: Since you are not convinced here's the snap

user3264953

Write this attribute android:descendantFocusability="blocksDescendants" in your mytask.xml relative layout.

And in main activity put a,c,b in place of "TaskId","heading","status".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!