Android:如何获取资源XML中定义的按钮的引用,而不是Activity XML中的按钮的引用

时间:2018-03-21 07:02:59

标签: java android onclicklistener

基本上我正在尝试为MainActivity.java中的按钮编写onClickListener。

此Button在ListView内的资源文件:main_resource.xml 中定义,而不在 activity_main.xml 中定义。

  

我收到错误:尝试在null上定义onClick侦听器   项目

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        myFancyMethod(v);
    }
})

这是我的main_resource.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="5dip">
    <TextView
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_width="wrap_content"
        android:id="@+id/ridedetails"
        android:layout_marginLeft="2dip">
    </TextView>

    <EditText
        android:layout_height="wrap_content"
        android:hint="Quote Price"
        android:layout_width="wrap_content"
        android:id="@+id/PriceQuotation"
        android:layout_below="@+id/ridedetails"
        android:layout_marginLeft="2dip">
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/PriceQuotation"
        android:layout_marginStart="61dp"
        android:layout_toEndOf="@+id/PriceQuotation"
        android:text="Button" />


</RelativeLayout>

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myrefresh"
    android:focusable="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_home"
        app:menu="@menu/activity_home_drawer" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".PreviousRide"
        >

        <ListView
            android:id="@+id/incomingrides"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="30dp"
            android:layout_marginTop="80dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"></ListView>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
</android.support.v4.widget.SwipeRefreshLayout>

这里我没有使用任何自定义适配器类:我使用以下用刷子刷新小工具的onRefresh()编写的ArrayAdapter代码将数据注入我的列表视图::

final ArrayAdapter<String > adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.home_resource,
                    R.id.ridedetails, allNames);
            l1.setAdapter(adapter);

// L1 is the listView Reference

2 个答案:

答案 0 :(得分:2)

您必须为适配器创建自定义类 以下是适配器的示例代码。

 public class MyAdapter extends ArrayAdapter<String> {
            private int resourceId;
            private List<String> sites = null;
            private Context context;

            public MyAdapter(Context context, int resource, List<String> objects) {
                super(context, resource, objects);
                this.context = context;
                this.resourceId = resource;
                this.sites = objects;
            }

            @Override
            public String getItem(int position) {
                return sites.get(position);
            }

            @Override
            public int getCount() {
                return sites.size();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                String name = getItem(position);
                View view = convertView;
                if (view == null) {
                    LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(resourceId, null);
                }
                TextView mTextView = (TextView) view.findViewById(R.id.ridedetails);
                mTextview.setText(name);
                Button mButton = (Button) view.findViewById(R.id.button);
                mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
                    }
                });

                return view;
            }
        }

在listview中设置适配器,如下面的代码。

l1.setAdapter(new MyAdapter(getApplicationContext(), R.layout.home_resource, allNames));

答案 1 :(得分:0)

你应该将onClickListener放在Adapter Not in Main Activity

您的适配器

    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            Button btn = (Button ) v.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        myFancyMethod(v);
    }
})

}