2017-01-10 5 views
0

私はこの問題についてここで見ましたが、私の答えは見つかりませんでした。OnItemClickListenerは特定のフラグメントでは機能しません

私の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

あなたはどこにでも 'showAllStuff(ID)'メソッドを呼び出していません。 –

+0

showAllStuff関数はどこで使用しますか? –

+0

申し訳ありませんが、私の質問にコピー/ペーストミスがありました。私はコードを修正します。私はoneCreateViewでそれを呼び出します。 –

答えて

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

} 
関連する問題