我在这里看到了这个问题,但我找不到答案。
我的onClickListener在以下片段中不起作用。但它适用于其他片段。如果听众有任何错误或者我忘记了某些事情,我不会害羞。
public class StuffFragment extends Fragment {
public View rootview;
private TravelDataSource dataSource;
private StuffDataSource dataSource_stuff;
public StuffFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.fragment_stuff, container, false);
id = getID();
getStuffList(id);
showAllStuff(id)
return rootview;
}
public void showAllStuff(long id) {
ArrayList<Stuff> arrayOfStuff = dataSource_stuff.getStuffForList(id, "false");
StuffAdapter adapter = new StuffAdapter(getContext(), arrayOfStuff);
ListView listView = (ListView) rootview.findViewById(R.id.listviewStuffList);
listView.setAdapter(adapter);
/*Fix für die Höhe*/
setListViewHeightBasedOnChildren(listView);
/*OnClick Listener*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Knobb gedrückt");
}
});
}
布局xml文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="de.christian_heinisch.packliste.StuffFragment"
android:orientation="vertical"
android:scrollbars="vertical"
android:nestedScrollingEnabled="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/padding">
<TextView
android:text="Stadtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewCityname"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textColor="@color/colorHeadline"
android:textSize="30sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="14.01.2017 - 21.01.2017"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewTravelDate"
android:layout_weight="1" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView7"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:text="Nicht vergessen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView10" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listviewStuffList" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Anzahl"
android:inputType="number"
android:id="@+id/editText_quantity"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editText_product"
android:layout_weight="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button_add_product" />
</LinearLayout>
<TextView
android:text="Muss noch gekauft werden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView12" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listviewStuffBuy"
android:fadingEdge="none"
android:scrollbars="none" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Anzahl"
android:inputType="number"
android:id="@+id/editText_quantity_buy"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editText_product_buy"
android:layout_weight="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/button_add_product_buy" />
</LinearLayout>
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
尝试代码:
public class StuffFragment extends Fragment {
public View rootview;
private ListView listView;
private TravelDataSource dataSource;
private StuffDataSource dataSource_stuff;
public StuffFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootview = inflater.inflate(R.layout.fragment_stuff, container, false);
listView = (ListView) rootview.findViewById(R.id.listviewStuffList);
id = getID();
getStuffList(id);
showAllStuff(id)
return rootview;
}
public void showAllStuff (long id) {
ArrayList<Stuff> arrayOfStuff = null;
arrayOfStuff = dataSource_stuff.getStuffForList(id, "false");
StuffAdapter adapter = new StuffAdapter(getContext(), arrayOfStuff);
listView.setAdapter(adapter);
/*Fix für die Höhe*/
setListViewHeightBasedOnChildren(listView);
/*OnClick Listener*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Knobb gedrückt");
}
});
}