Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何获取在资源XML而不是活动XML中定义的按钮的引用_Java_Android_Onclicklistener - Fatal编程技术网

Java Android:如何获取在资源XML而不是活动XML中定义的按钮的引用

Java Android:如何获取在资源XML而不是活动XML中定义的按钮的引用,java,android,onclicklistener,Java,Android,Onclicklistener,基本上,我试图为MainActivity.java中的按钮编写一个onClickListener 此按钮在资源文件中定义:main\u resource.xml在列表视图中,而不是在活动\u main.xml中 我得到一个错误: 正在尝试在null项上定义onClick侦听器 以下是我的主要资源_resource.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout

基本上,我试图为MainActivity.java中的按钮编写一个
onClickListener

此按钮在资源文件中定义:main\u resource.xml
列表视图中,而不是在活动\u main.xml

我得到一个错误:

正在尝试在null项上定义onClick侦听器

以下是我的主要资源_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>

以下是我的活动_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>

这里我没有使用任何自定义适配器类:我使用以下ArrayAdapter代码将数据注入我的列表视图,该代码是在Swipe Refresh小部件的onRefresh()中编写的:

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

// L1 is the listView Reference
final ArrayAdapter adapter=new ArrayAdapter(getApplicationContext(),
R.layout.home\u资源,R.id.ridedetails,allNames);
l1.设置适配器(适配器);
//L1是listView引用

您应该将onClickListener放在适配器中,而不是主活动中

您的适配器

    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);
    }
})

}

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

 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;
            }
        }

编辑:使用适当的xml我知道在按钮位于activity_main.xml中时定义按钮onClickListeners,但无法确定如何在此场景中继续放置适配器代码。如果在主活动布局中也包含该布局,则只有一种方法可以在活动中使用该按钮的id。否则这是一个很大的问题:)当使用listview时,您应该使用adapter。我没有通过扩展AdapterView类创建任何自定义适配器类。我是android新手,你能帮我看一下我问题的最后一部分吗。谢谢但是为什么字符串名称=getItem(位置);始终返回列表最后一项的内容,而不是返回指定位置的项。
 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;
            }
        }
l1.setAdapter(new MyAdapter(getApplicationContext(), R.layout.home_resource, allNames));