J'ai une application, avec l'activité de tiroir de navigation, l'activité principale est vide et dans les fragments j'ai une liste avec des images. La mise en page est:Liste dans un fragment avec l'activité 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"
/>
tout dans un fragment.
alors j'ai un constructeur:
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; }}
puis dans mon fragment j'ai:
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);
}
}
j'ai un autre, mais qui fonctionne très bien, mais avec des rootView et la principale activité définit les paramètres. Tout fonctionne très bien, mais quand je clique dans une liste, il ne fonctionne pas. ***GetaplicationContext()***
c'est en couleur rouge, c'est une erreur. J'ai essayé avec getActivity(), *** avec getActivityGetapplicationContet(),***
l'erreur disparaît mais cela ne fonctionne pas. Je pense que le rootView c'est le problème. une idée de l'erreur ou d'un autre type de code pour le faire? merci pour tout.
Faites-moi savoir si cela ne fonctionne pas pour vous !! –
merci pour votre réponse. J'ai mis votre code dans mon adaptateur/constructeur et j'ai une erreur, "ne peut pas résoudre getApplicationContext()", et rowView doit être déclaré final " quelques idées? –
J'ai fait des modifications. S'il vous plaît vérifier maintenant et accepter la réponse Si cela fonctionne !! Merci –