如何为自定义列表视图中的按钮实现 "On click"
how to implemet "On click" for a button which is in custom list view
我在自定义列表视图中有三个按钮,列表视图位于片段中而不是 activity,但是当我设置 onclick listner 时。它抛出异常 null 对象引用已传递。
custome_list_view.xml
<Button
android:text="@string/pickUpButtonText"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="@dimen/standard_hight"
android:background="@drawable/buttonshape"
android:shadowColor="#5BA84F"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:id="@+id/button_pickup_list"
/>
customeListView.Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jobs, container, false);
ArrayList<JobListDataProvider> listJobs = GetRequests();
ListView lv = (ListView)rootView.findViewById(R.id.listViewJobs);
lv.setAdapter(new JobsListAdapter(getActivity(), listJobs));
//.....bindind on click....
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//......................
}
});
return rootView;
}
你必须把这段代码放在 JobsListAdapter
in public View onCreateView
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//......................
}
});
我在自定义列表视图中有三个按钮,列表视图位于片段中而不是 activity,但是当我设置 onclick listner 时。它抛出异常 null 对象引用已传递。 custome_list_view.xml
<Button
android:text="@string/pickUpButtonText"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="@dimen/standard_hight"
android:background="@drawable/buttonshape"
android:shadowColor="#5BA84F"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:id="@+id/button_pickup_list"
/>
customeListView.Java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jobs, container, false);
ArrayList<JobListDataProvider> listJobs = GetRequests();
ListView lv = (ListView)rootView.findViewById(R.id.listViewJobs);
lv.setAdapter(new JobsListAdapter(getActivity(), listJobs));
//.....bindind on click....
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//......................
}
});
return rootView;
}
你必须把这段代码放在 JobsListAdapter
in public View onCreateView
Button pickup = (Button) rootView.findViewById(R.id.button_pickup_list);
pickup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//......................
}
});