Activity+ArrayList+Adapter vers Fragment
Bonjour,
J'avais une activity au début qui affichait une liste d'objet:
LastNoveActivity.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package novtonrak.perstud.com.novtonrak.lastNove;
import android.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import novtonrak.perstud.com.novtonrak.R;
public class LastNoveActivity extends AppCompatActivity implements LastNoveAdapterListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lastNove);
ArrayList<LastNove> listLastNove = LastNove.getAListOfLastNove();
LastNoveAdapter adapter = new LastNoveAdapter(this, listLastNove);
adapter.addListener(this);
ListView list = (ListView)findViewById(R.id.ListView01);
list.setAdapter(adapter);
}
@Override
public void onClickButtonImage(LastNove item, int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("LastNove");
builder.setMessage("Vous avez clique sur : " + item.id_LastNove);
builder.setPositiveButton("Oui", null);
builder.setNegativeButton("Non", null);
builder.show();
}
} |
activity_last_nove.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="novtonrak.perstud.com.novtonrak.lastNove.LastNoveActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frameLayout"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Annonces d'animals perdus :"
android:id="@+id/textView_label_annonce_perdu"
android:gravity="center_horizontal" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/frameLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_label_annonce_perdu"
android:background="@drawable/rectangle">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rectangle"
android:gravity="center_vertical|center_horizontal">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout> |
J'ai donc déporter cette activité vers un fragment mais je n'arrive pas à "déporter" la fonction onClickButtonImage() de l'interface "LastNoveAdapterListener", ni appeler la méthode findViewById() pour récupérer ma List.
Voici le code mon fragment (avec erreur) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| package novtonrak.perstud.com.novtonrak;
import android.app.AlertDialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import novtonrak.perstud.com.novtonrak.lastnove.Lastnove;
import novtonrak.perstud.com.novtonrak.lastnove.LastnoveAdapter;
import novtonrak.perstud.com.novtonrak.lastnove.LastnoveAdapterListener;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link .OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link tab1_LastNoveFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class tab1_LastNoveFragment extends Fragment /*implements LastnoveAdapterListener*/ {
// 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 tab1_LastNoveFragment() {
// 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 tab1_LastNoveFragment.
*/
// TODO: Rename and change types and number of parameters
public static tab1_LastNoveFragment newInstance(String param1, String param2) {
tab1_LastNoveFragment fragment = new tab1_LastNoveFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
ArrayList<Lastnove> listLastnove = Lastnove.getAListOfLastnove();
LastnoveAdapter adapter = new LastnoveAdapter(this, listLastnove); // erreur de paramètre (this, listLastNove) --> cannot be applied
adapter.addListener(this); // erreur de paramètre this --> cannot be applied
ListView list = (ListView)findViewById(R.id.ListView01); // cannot resolve method findviewbyid()
list.setAdapter(adapter);
}
@Override
public void onClickButtonImage(Lastnove item, int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); // erreur de paramètre this --> cannot be applied
builder.setTitle("Lastnove");
builder.setMessage("Vous avez clique sur : " + item.id_Lastnove);
builder.setPositiveButton("Oui", null);
builder.setNegativeButton("Non", null);
builder.show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab1_last_nove, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
Log.w("LAstnoveFrag", "OnAttachCalled");
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;
}
} |
Voici le code des éléments qui posent problème lors de l'insertion dans le fragment:
J'ai tenté de mettre çà dans 'onCreate()' et 'onCreateView()' mais 'LastNoveAdapter(this, listLastNove)', addListenner(this) et 'findViewById' génèrent une erreur
Code:
1 2 3 4 5 6 7
| ArrayList<LastNove> listLastNove = LastNove.getAListOfLastNove();
LastnoveAdapter adapter = new LastnoveAdapter(this, listLastnove); // erreur de paramètre (this, listLastNove) --> cannot be applied
adapter.addListener(this); // erreur de paramètre this --> cannot be applied
ListView list = (ListView)findViewById(R.id.ListView01); // cannot resolve method findviewbyid()
list.setAdapter(adapter); |
Je dois implémenter cette fonction provenant de l'interface, le passage de (this) à cette ligne pose également problème:
Citation:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
Code:
1 2 3 4 5 6 7 8 9 10
| @Override
public void onClickButtonImage(LastNove item, int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); // erreur de paramètre this --> cannot be applied
builder.setTitle("LastNove");
builder.setMessage("Vous avez clique sur : " + item.id_LastNove);
builder.setPositiveButton("Oui", null);
builder.setNegativeButton("Non", null);
builder.show();
} |
Comment puis je afficher ma list dans le fragment plutôt que dans mon activity svp ?