2017-08-25 68 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

在項目上實現您的點擊偵聽器而不是偵聽。在適配器類的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

感謝您的回覆。我把你的代碼放到我的Adapter /構造函數中,我有一些錯誤,「不能解決getApplicationContext()」,並且rowView必須聲明爲最後的「 一些想法? –

+0

我已經做了編輯,請現在檢查並接受答案如果有效!!謝謝 –