私はリストビューを持っているアプリケーションで作業しています。リストビューには、異なるレイアウトファイルにあるボタンがあります。自分のアクティビティからボタンを呼び出すにはどうすればいいですか?そのxmlファイルを自分のアクティビティに追加しようとしましたが、setOnClickListenerは機能していないようです。他のレイアウトのボタンをクリックする
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:orientation="horizontal"
android:padding="20dp">
<EditText
android:id="@+id/objectiveOtherAddedEditText"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginLeft="28dp"
android:ems="10" />
<ImageButton
android:id="@+id/addOtherObjectiveTextBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/round_button"
android:src="@android:drawable/ic_input_add"/>
</LinearLayout>
と私はそれを呼びたい場所です::
これは私のxmlファイルがどのように見えるかです
public class VisitObjectiveFragment extends Fragment {
public static final String TITLE = "Visit Objective";
SQLiteDatabase database;
SQLiteOpenHelper dbHelper;
public static VisitObjectiveFragment newInstance() {
return new VisitObjectiveFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_visit_objective, container, false);
final View view1 = inflater.inflate(R.layout.other_objective_list_view, container, false);
ListView listView = (ListView) view.findViewById(R.id.listview_objective);
ImageButton buttonAdd = (ImageButton) view1.findViewById(R.id.addOtherObjectiveTextBox);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
リストビューの各行を拡張するために使用されているアダプタのレイアウトが他のレイアウトであることを意味しますか? –
あなたが 'ImageButton buttonAdd =(ImageButton)view1.findViewById(R.id.addOtherObjectiveTextBox); 'で述べたIDは、XMLにはどこにも見つかりません。 ImageButtonで言及したXML IDは、 'android:id =" @ + id/addNewOtherObjectiveTextBox "' –
@AnindyaDuttaです。私が持っているのは、2つの異なるレイアウトを膨らませるリストビューです。 1つのレイアウトにはチェックボックスがあり、もう1つのレイアウトには画像ボックス付きの入力ボックスがあります。 –