Android cannot find method from button's onClick in ListView Row

后端 未结 4 1901
甜味超标
甜味超标 2021-01-18 23:10

I have a ListView populated from a custom adapter. Each row has 1 button in it. In the xml the button has the onClick attribute passed. I have only the xml, not any OnClickL

相关标签:
4条回答
  • 2021-01-18 23:53

    It’s important that MyActivity and getContext() of CustomAdapter must be the same instance. Compare yours with mine.

    My codes:

    MyActivity.java

    public class MyActivity extends Activity {
        public static final String TAG = "MyActivity";
        private ListView mListView;
        private CustomAdapter mAdapter;
        private ArrayList<String> mData;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
            mListView = (ListView) findViewById(R.id.listView);
    
            mData = new ArrayList<String>();
            mData.add("111");
            mData.add("222");
            mData.add("333");
            mData.add("444");
            mData.add("555");
    
            mAdapter = new CustomAdapter(this, R.layout.list_item_view, mData);
            mListView.setAdapter(mAdapter);
        }
    
        public void onClickHandler(View view) {
            Log.i(TAG, "onClickHandler()");
        }
    }
    

    CustomAdapter.java

    public class CustomAdapter extends ArrayAdapter<String> {
    
        public CustomAdapter(Context context, int resource, ArrayList<String> objects) {
            super(context, resource, objects);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.list_item_view, null);
            }
    
            return convertView;
        }
    }
    

    activity_my.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MyActivity">
    
        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </RelativeLayout>
    

    list_item_view.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:id="@+id/button"
            android:onClick="onClickHandler"/>
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-18 23:57

    This is one example of why the onClick attribute is considered broken. It is probably best to create a custom Adapter and in the getView() method, set the OnClickListener manually.

    0 讨论(0)
  • 2021-01-19 00:05

    I'm assuming you wrote a custom adapter for this view so in your adapter when you call getView simply findElementById on the button and set the onClickListener there.

    0 讨论(0)
  • 2021-01-19 00:06

    When creating the custom Adapter, I was passing as the Context the result of the getApplicationContext() method. This was wrong. I should pass this (my custom Activity) as the Context. It works like a charm now.

    0 讨论(0)
提交回复
热议问题