2017-08-25 8 views
0

ナビゲーション・ドロワー・アクティビティがあるアプリを持っています。それは空のメイン・アクティビティです。フラグメントの中には画像のリストがあります。レイアウトは、すべてのフラグメントでNavigationdrawerアクティビティ、itemSelectedを含むフラグメントのリスト

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.example.iblancogonzalez.innch.Cocktail"> 
<LinearLayout 
      android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
android:background="@drawable/post_border_style" 
     > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:padding="5dp" /> 
    <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:gravity="center" > 

<TextView 
     android:id="@+id/texto_principal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="5dp" 
     android:padding="2dp" 
     android:textColor="#ff020f" /> 
     <TextView 
     android:id="@+id/texto_secundario" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:layout_marginLeft="10dp" 
     android:textColor="#0db112" /> 
</LinearLayout> 
</LinearLayout> 
    <ListView 
    android:id="@+id/mi_lista" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

。その後、

私はコンストラクタがあります。

public class AdaptadorCocktail extends ArrayAdapter<String> { 

private final Activity context; 
private final String[] itemname; 

private final Integer[] integers; 


public AdaptadorCocktail(Activity context, String[] itemname, Integer[] integers) { 
    super(context, R.layout.fragment_cocktail, itemname); 

    this.context=context; 
    this.itemname=itemname; 
    //this.itemexpl=itemexpl; 
    this.integers=integers; 
} 


public View getView(int posicion,View view, ViewGroup parent){ 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //LayoutInflater inflater=context.getLayoutInflater(); 

    View rowView=inflater.inflate(R.layout.fragment_cocktail,null,true); 

    TextView txtTitle = (TextView) rowView.findViewById(R.id.texto_principal); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    TextView etxDescripcion = (TextView) rowView.findViewById(R.id.texto_secundario); 

    txtTitle.setText(itemname[posicion]); 
    imageView.setImageResource(integers[posicion]); 
      return rowView; }} 

とし、その後、私のフラグメントに私が持っている:

public class Cocktail extends Fragment { 
private String lenguajeProgramacion[]=new String[]{"Bolum Crujiente de morcilla, queso, manzana y piña","Bolum de foie, nuees y gelatina de vino tinto","Bolum de foie con almendra caramelizada y mango","Chulipop de queso Idiazabal","Chulipop de salmon con queso manchego","Chulipop de foie con chocolate","Macarron de foie"}; 
private String itemexplc[]=new String[]{"(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 14g, 30 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 15g, 40 unidades por estuche, caja de 6 estuches)","(Formato 10g, 48 unidades por estuche, caja de 6 estuches)"}; 

View rootView; 
private Integer[] imgid={R.mipmap.bola, 
     R.mipmap.bolb,R.mipmap.bolc,R.mipmap.chula,R.mipmap.chulb,R.mipmap.chulc,R.mipmap.maca}; 
private ListView lista; 


// TODO: Rename parameter arguments, choose names that match 
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
private static final String ARG_PARAM1 = "param1"; 
private static final String ARG_PARAM2 = "param2"; 

// TODO: Rename and change types of parameters 
private String mParam1; 
private String mParam2; 

private OnFragmentInteractionListener mListener; 

public Cocktail() { 

    // Required empty public constructor 
} 

/** 
* Use this factory method to create a new instance of 
* this fragment using the provided parameters. 
* 
* @param param1 Parameter 1. 
* @param param2 Parameter 2. 
* @return A new instance of fragment Cocktail. 
*/ 
// TODO: Rename and change types and number of parameters 
public static Cocktail newInstance(String param1, String param2) { 
    Cocktail fragment = new Cocktail(); 
    Bundle args = new Bundle(); 
    args.putString(ARG_PARAM1, param1); 
    args.putString(ARG_PARAM2, param2); 
    fragment.setArguments(args); 
    return fragment; 
} 



@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 


    // Inflate the layout for this fragment 

    rootView = inflater.inflate(R.layout.fragment_cocktail, container, false); 

    AdaptadorCocktail adapter = new AdaptadorCocktail(getActivity(),lenguajeProgramacion,imgid); 

    lista=(ListView)rootView.findViewById(R.id.mi_lista); 
    lista.setAdapter(adapter); 
    Toast.makeText(getActivity(),"ojo1", Toast.LENGTH_SHORT).show(); 
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      String Slecteditem= lenguajeProgramacion[+position]; 
      Toast.makeText(getApplicationContext(),Slecteditem, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return rootView;} 







    public void onButtonPressed(Uri uri) { 
    if (mListener != null) { 
     mListener.onFragmentInteraction(uri); 
    } 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    if (context instanceof OnFragmentInteractionListener) { 
     mListener = (OnFragmentInteractionListener) context; 
    } else { 
     throw new RuntimeException(context.toString() 
       + " must implement OnFragmentInteractionListener"); 
    } 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mListener = null; 
} 



/** 
* This interface must be implemented by activities that contain this 
* fragment to allow an interaction in this fragment to be communicated 
* to the activity and potentially other fragments contained in that 
* activity. 
* <p> 
* See the Android Training lesson <a href= 
* "http://developer.android.com/training/basics/fragments/communicating.html" 
* >Communicating with Other Fragments</a> for more information. 
*/ 
public interface OnFragmentInteractionListener { 
    // TODO: Update argument type and name 
    void onFragmentInteraction(Uri uri); 
} 
} 

私は他のものを持っているが、それは非常によく機能しますが、アウトrootViewとメインでアクティビティはパラメータを定義します。 すべて非常にうまく動作しますが、リスト内をクリックすると動作しません。 ***GetaplicationContext()***赤い色ですが、間違いです。私は試しましたgetActivity()、***とgetActivityGetapplicationContet(),***エラーdesapearで動作しません。 私はrootViewが問題だと思います。 エラーやそれを行うための他の種類のコードのアイデアですか? ありがとうございます。

答えて

0

listeviewの代わりにアイテムに対してクリックリスナーを実装します。このようなアダプタクラスのgetView()メソッド内に実装してください -

public View getView(int posicion,View view, ViewGroup parent){ 

LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

//LayoutInflater inflater=context.getLayoutInflater(); 

final View rowView=inflater.inflate(R.layout.fragment_cocktail,null,true); 

TextView txtTitle = (TextView) rowView.findViewById(R.id.texto_principal); 
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
TextView etxDescripcion = (TextView) rowView.findViewById(R.id.texto_secundario); 

txtTitle.setText(itemname[posicion]); 
imageView.setImageResource(integers[posicion]); 
rowView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      //put message here 
      String Slecteditem= lenguajeProgramacion[+position]; 
      Toast.makeText(context, Slecteditem, Toast.LENGTH_SHORT).show();} 
      return rowView; }} 
} 
+0

これはあなたのために動作しない場合は教えてください! –

+0

お返事ありがとうございます。私はあなたのコードを私のアダプタ/コンストラクタに入れて、 "getApplicationContext()を解決できません"というエラーがあり、最後に宣言しなければならない " いくつかのアイデア? –

+0

私は編集を行いました。 –

関連する問題