IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Problème de rendu sur une application


Sujet :

Android

  1. #1
    Membre actif
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 604
    Points : 206
    Points
    206
    Par défaut Problème de rendu sur une application
    Bonjour,
    J'ai une classe Tache qui attend un titre (string) et une priorité (int), dans ma première vue j'ai une listView et un bouton ajouter, j'affiche les taches dans la listView et quand je clique sur ajouter je pars sur une deuxième vue où on peut ajouter une nouvelle tache, dans la deuxième j'ai un bouton annuler et un bouton sauver qui reviennent sur la première vu.

    Mes problèmes sont :
    - quand je lance mon programme j'ai une tache vide qui est ajouter
    - quand j'ajoute une tache (dans la deuxième vue) et que clique sur sauver je ne reviens pas dans la première vue mais la tache a bien été ajouté
    - si j'ajoute une nouvelle fois une nouvelle tache, elle remplace la tache précédemment ajouté.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
    public class Tache
    {
     
        String titre ;
        int priorite ;
        int imgPiorite;
     
     
     
        public Tache (String unTitre, int unePriorite)
        {
            this.titre = unTitre;
            this.priorite = unePriorite;
            this.imgPiorite = imgPriorite();
     
     
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class MainActivity extends ListActivity {
     
        ArrayList<Tache> liste_taches = new ArrayList<Tache>();
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            //setContentView(R.layout.test);
     
            liste_taches.add(new Tache("Cours de programmation",4));
            liste_taches.add(new Tache("Entraînement de natation",3));
            liste_taches.add(new Tache("Faire une sieste",2));
            liste_taches.add(new Tache("Prendre une douche",0));
            liste_taches.add(new Tache("Ecouter des infos",1));
     
            AdaptateurPerso adapter = new AdaptateurPerso(this,liste_taches);
            setListAdapter(adapter);
     
            Button ajouter = (Button) findViewById(R.id.ajout);
            ajouter.setOnClickListener(ajoutOnClick);
     
            Intent intent = getIntent();
     
            liste_taches.add(new Tache(intent.getStringExtra("titre"),intent.getIntExtra("priorite",0)));
     
        }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class AdaptateurPerso extends ArrayAdapter<Tache> {
     
        private final Context context;
     
     
     
     
        AdaptateurPerso(Context c, ArrayList<Tache> tableau_taches)
        {
            super(c,R.layout.single_row,R.id.item,tableau_taches);
            this.context = c;
     
     
     
        }
     
        public View getView(int position, View convertView, ViewGroup parent )
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.single_row,parent,false);
     
            ImageView IVPrio = (ImageView) row.findViewById(R.id.imageView);
            TextView TVItem = (TextView) row.findViewById(R.id.item);
            TextView TVItem_sub = (TextView) row.findViewById(R.id.sub_item);
     
            IVPrio.setImageResource(getItem(position).imgPiorite);
            TVItem.setText(getItem(position).titre);
            TVItem_sub.setText("priorite: "+getItem(position).priorite);
     
            return row;
        }
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    public class ActivityAjoutTache extends Activity {
     
        final String clesTitre = "titre";
        final String clesPriorite = "priorite";
     
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ajout_tache);
     
            Button annuler  = (Button) findViewById(R.id.annuler);
            annuler.setOnClickListener(annuleOnClick);
     
            Button sauve    = (Button) findViewById(R.id.sauver);
            annuler.setOnClickListener(sauveOnClick);
     
        }
     
        private View.OnClickListener annuleOnClick = new View.OnClickListener() {
     
            public void onClick(View v) {
     
                Intent intent = new Intent(v.getContext(),MainActivity.class);
                startActivity(intent);
     
            }
        };
     
        private View.OnClickListener sauveOnClick = new View.OnClickListener() {
     
            public void onClick(View v) {
     
                Intent intent        = new Intent(v.getContext(),MainActivity.class);
     
                EditText varTitre    =  (EditText) findViewById(R.id.titre);
                EditText varPriorite = (EditText) findViewById(R.id.priorite);
     
                String StrTitre     = varTitre.getText().toString();
                String StrPriorite  = varPriorite.getText().toString();
     
                try  {
     
                    int intPriorite = Integer.parseInt(StrPriorite);
     
                    intent.putExtra(clesTitre,StrTitre);
                    intent.putExtra(clesPriorite,intPriorite);
     
                }
     
                catch (NumberFormatException nfe) {}
     
                startActivity(intent);
     
     
     
            }
        };
     
    }
    Deuxième activité : activity_ajout_tache.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
     
     
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Titre"
                android:id="@+id/textView"
                android:textStyle="bold"
                android:textColor="#ff000000" />
     
            <EditText
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/titre"
                android:text="Entrer un titre"
                android:background="#ff8c8b8b" />
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Priorité"
                android:id="@+id/textView3"
                android:textStyle="bold"
                android:textColor="#ff000000" />
     
            <EditText
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Ajouter un priorité"
                android:id="@+id/priorite"
                android:background="#ff8c8b8b" />
     
            <LinearLayout
                android:layout_gravity="right"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
     
                <Button
                    android:layout_marginRight="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Annuler"
                    android:id="@+id/annuler" />
     
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sauver"
                    android:id="@+id/sauver" />
            </LinearLayout>
     
        </LinearLayout>
    </LinearLayout>
    Premiere activité : activity_main.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
     
        <ListView
     
            android:layout_above="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/list"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
     
        <LinearLayout
     
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="70dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:gravity="bottom"
            android:id="@+id/linearLayout">
     
            <Button
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Ajouter une \n tâche"
                android:id="@+id/ajout"
                android:textAlignment="center"
                android:onClick="ajoutOnClick" />
     
            <Button
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Quitter"
                android:id="@+id/quitter"
                android:textAlignment="center" />
        </LinearLayout>
     
     
     
     
    </RelativeLayout>
    va avec la listView : single_row.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
     
     
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/linearLayout">
     
            <ImageView
                android:layout_margin="15dp"
                android:src="@drawable/prio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imageView" />
        </LinearLayout>
     
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/linearLayout"
            android:layout_toEndOf="@+id/linearLayout">
     
            <TextView
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/item" />
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/sub_item" />
     
        </LinearLayout>
    </RelativeLayout>

  2. #2
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 64
    Points : 96
    Points
    96
    Par défaut
    Bonsoir,

    quand je lance mon programme j'ai une tache vide qui est ajouter
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Intent intent = getIntent();
    liste_taches.add(new Tache(intent.getStringExtra("titre"),intent.getIntExtra("priorite",0)));
    Quand tu démarres, ton intent est vide aussi...puis (juste à la suite) tu ajoutes dans ta liste Tache(null, null). Aucun soucis de ce côté là^^

    quand j'ajoute une tache (dans la deuxième vue) et que clique sur sauver je ne reviens pas dans la première vue mais la tache a bien été ajouté
    si j'ajoute une nouvelle fois une nouvelle tache, elle remplace la tache précédemment ajouté.
    A chaque fois que tu appuies sur tes bouton tu fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     Intent intent = new Intent(truc.class); 
    startActivity(intent);
    Si tu démarres une nouvelle activité, à chaque fois, tu as une nouvelle liste (vide).

    Conclusion -> rien d'anormal !

    Je te propose une solution (je fais au plus simple, tu modifieras comme bon te semble) :
    - Une activité principale contenant une ListView et un bouton pour ajouter un nouvel élément.
    - Le bouton ajouter n'ouvre plus une nouvelle activité mais un "DialogFragment" (une petite fenêtre qui passe au-dessus de ton activité).

    L'activité principale :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class MainActivity extends ActionBarActivity implements OnFinishedDialFrag {
     
        ArrayList<Tache> liste_taches;
        ListView mListView;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            liste_taches = new ArrayList<>();
     
            CustomListAdapter adapter = new CustomListAdapter(this, R.layout.item_row, liste_taches); //adaptateur pour afficher les éléments de la liste dans la ListView
            mListView = (ListView) findViewById(R.id.list_custom);
            mListView.setAdapter(adapter);
        }
     
        public void DoSomething(View v){ //fonction qu'appelle le bouton ajouter
            DialogFragment df = DialFrag.newInstance(); //déclare un DialogFrament
            df.show(getFragmentManager(), "dialog"); //ouvre le DialogFragment
        }
     
        @Override
        public void onFinishedDialFrag(String title, int priority) { //méthode appelée lorsque l'on ferme la fenêtre (DialogFragment) "ajouter" en appuyant sur le bouton valider de la fenêtre
            liste_taches.add(new Tache(title, priority)); //je crée une nouvelle tache avec le contenu des EditText de la fenêtre
        }
    }
    xml associée à cette activité, rien de plus basique une Listview et un bouton en dessous :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <LinearLayout 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:orientation="vertical"
        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=".MainActivity">
     
        <ListView
            android:id="@+id/list_custom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
     
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="DoSomething"
            android:text="Add"
            android:textSize="16sp" />
     
    </LinearLayout>
    Une interface qui sert de listener quand on ferme le DialogFragment (j'ai l'habitude de faire un .java à part mais tu peux tout aussi bien l'incorporer dans un .java existant surtout pour trois lignes^^) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    public interface OnFinishedDialFrag {
        void onFinishedDialFrag(String title, int priority); //j'ai deux paramètres, car il y aura deux EditText dans ma fenêtre (DialogFragment)
                                                                                //, ce sont les info que j'ai besoin "d'envoyer" à mon activité principale.
    }
    L'adaptateur pour afficher les taches dans la ListView :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class CustomListAdapter extends ArrayAdapter<Tache> {
        private Context context;
        private int resource;
        private ArrayList<Tache> list;
     
        public CustomListAdapter(Context context, int resource, ArrayList<Tache> list) {
            super(context, resource, list);
            this.context = context;
            this.resource = resource;
            this.list = list;
        }
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            RecordHolder holder;
     
            if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(resource, parent, false);
     
     
                holder = new RecordHolder();
                holder.tv_item = (TextView) row.findViewById(R.id.item);
                holder.tv_subitem = (TextView) row.findViewById(R.id.sub_item);
     
                row.setTag(holder);
            } else
                holder = (RecordHolder) row.getTag();
     
            Tache tache = list.get(position);
            holder.tv_item.setText(tache.getTitre());
            holder.tv_subitem.setText(tache.getPriorite()+"");
     
            return row;
        }
     
        static class RecordHolder {
            TextView tv_item;
            TextView tv_subitem;
        }
    }
    Maintenant il nous faut faire la fenêtre, une class qui étend DialogFragment

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class DialFrag extends DialogFragment {
        EditText ed_title;
        EditText ed_subitem;
        Button button_add;
     
        public static DialFrag newInstance() {
            return new DialFrag();
        }
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
     
            View v = inflater.inflate(R.layout.frag_dial, container, false);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            ed_title = (EditText) v.findViewById(R.id.edit_title);
            ed_subitem = (EditText) v.findViewById(R.id.edit_other);
            button_add = (Button) v.findViewById(R.id.button); //le bouton qui permet de valider l'ajouter
            button_add.setOnClickListener(new View.OnClickListener() { //listener associé au bouton
                @Override
                public void onClick(View v) {
                    OnFinishedDialFrag activity = (OnFinishedDialFrag) getActivity(); //on crée un listener sur l'activité principale en passant par l'interface
                    activity.onFinishedDialFrag(ed_title.getText().toString(), Integer.parseInt(ed_subitem.getText().toString())); //on "envoie" nos info, dans ce cas le contenu des EditText.
                    getDialog().dismiss(); // je ferme la fenêtre
                }
            });
     
            return v;
        }
    }
    xml associé à la fenêtre, rien de particulier deux EditText et un bouton :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp">
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titre"
                android:textSize="18sp"/>
     
            <EditText
                android:id="@+id/edit_title"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="Entrez un titre"
                android:inputType="text" />
        </LinearLayout>
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titre"
                android:textSize="18sp" />
     
            <EditText
                android:id="@+id/edit_other"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="Entrez un titre"
                android:inputType="text"/>
        </LinearLayout>
     
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Valider"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>
    Au final cela doit donner ceci :
    Pièce jointe 175295

    J'espère que c'est compréhensible, n'hésite à demander d'avantage d'explications dans le cas contraire.

    Cordialement,
    Nicolas

  3. #3
    Membre actif
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 604
    Points : 206
    Points
    206
    Par défaut
    Bonjour,
    En fait l'exercice consiste a manipuler des éléments à travers des vues, donc il faut que je fasse deux vues mais le fait de faire une boite de dialogue me servira pour les exos futurs ^^.
    Mais si non j'ai des erreurs :
    dans MainActivity au niveau de implements OnFinishedDialFrag : Cannot resolve symbol OnFinishedDialFrag
    (si je fais alt+entré sur OnFinishedDialFrag il me met MainActivity.OnFinishedDialFrag : erreur : Class must either be declared abstract or implement abstract method
    dans LainActivity dans la méthode onCreate au niveau de protected void onCreate dans new CustomListAdapter(this, R.layout.item_row, liste_taches): Cannot resolve symbol item_row
    dans la classe onFinishedDialFrag : Cannot resolve symbol liste_taches

  4. #4
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 64
    Points : 96
    Points
    96
    Par défaut
    Bonjour,
    As tu créé l'interface "OnFinishedDialFrag" (personnellement j'ai fais un fichier à part nommé OnFinishedDiaFrag.java) et implémenté la class MainActivity avec l'interface ?
    Il faut aussi ajouter ceci dans la fichier MainActivity.java (tu as peut-être oublié cette méthode) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    @Override
    public void onFinishedDialFrag(String title, int priority) { //méthode appelée lorsque l'on ferme la fenêtre (DialogFragment) "ajouter" en appuyant sur le bouton valider de la fenêtre
            liste_taches.add(new Tache(title, priority)); //je crée une nouvelle tache avec le contenu des EditText de la fenêtre
    }
    J'ai testé avant de poster et je n'ai pas eu de soucis.


    Sinon si tu dois absolument le faire avec deux activités, il faut utiliser les methodes startActivityForResult(Intent, int) au lieux de startActivity(Intent) et récupérer le résultat avec la méthode "protected' onActivityResult().
    Voici la démarche : http://developer.android.com/trainin...ts/result.html

  5. #5
    Membre actif
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 604
    Points : 206
    Points
    206
    Par défaut
    J'ai rajouté protected void onActivityResult mais quand je clique sur le bouton sauver je ne pars pas dans la première et je ne sais pas comment faire pour que les données initiales ne se rajoute pas dans de nouveau dans la liste_tache quand je reviens sur la première vue (quand je sauve dans la liste_tache les données initiales se rajoutent de nouveau)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
     
    public class ActivityAjoutTache extends Activity {
     
        final String clesTitre = "titre";
        final String clesPriorite = "priorite";
     
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ajout_tache);
     
            Button annuler  = (Button) findViewById(R.id.annuler);
            annuler.setOnClickListener(annuleOnClick);
     
            Button sauve    = (Button) findViewById(R.id.sauver);
            annuler.setOnClickListener(sauveOnClick);
     
        }
     
        private View.OnClickListener annuleOnClick = new View.OnClickListener() {
     
            public void onClick(View v) {
     
                Intent intent = new Intent(v.getContext(),MainActivity.class);
                startActivity(intent);
     
            }
        };
     
        private View.OnClickListener sauveOnClick = new View.OnClickListener() {
     
            public void onClick(View v) {
     
                Intent intent        = new Intent(v.getContext(),MainActivity.class);
     
                EditText varTitre    =  (EditText) findViewById(R.id.titre);
                EditText varPriorite = (EditText) findViewById(R.id.priorite);
     
                String StrTitre     = varTitre.getText().toString();
                String StrPriorite  = varPriorite.getText().toString();
     
                try  {
     
                    int intPriorite = Integer.parseInt(StrPriorite);
     
                    intent.putExtra(clesTitre,StrTitre);
                    intent.putExtra(clesPriorite,intPriorite);
     
                }
     
                catch (NumberFormatException nfe) {}
     
                startActivityForResult(intent, 1);
     
     
     
            }
        };
     
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            super.onActivityResult(requestCode,resultCode,data);
     
            if (requestCode == 1)
            {
                if (data != null)
                {
                    Intent intent = getIntent();
                    MainActivity.liste_taches.add(new Tache(intent.getStringExtra("titre"),intent.getIntExtra("priorite",0)));
     
                }
            }
     
        }
     
     
     
    }

  6. #6
    Membre régulier
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2014
    Messages
    64
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2014
    Messages : 64
    Points : 96
    Points
    96
    Par défaut
    ça ne fonctionne pas comme cela. Lorsque qu'il y a startActivityForResult() l'activité attend un résultat de l'activité qu'elle ouvre. Cette seconde activité lui répond par setResult(int code, Intent intent) avec code qui indique si ça s'est bien passé ou bien si ça a été annulé.

    MainActivity.java (activité contenant un bouton ajout et une listview)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class MainActivity extends ActionBarActivity {
        Button b_add;
        Activity activity;
        ListView mListView;
        ArrayList<Tache> mlist;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            activity = this;
            mlist = new ArrayList<>();
            mListView = (ListView) findViewById(R.id.list_custom);
            CustomListAdapter adapter = new CustomListAdapter(this, R.layout.item_row, mlist);
            mListView = (ListView) findViewById(R.id.list_custom);
            mListView.setAdapter(adapter);//attache l'adaptateur
     
            b_add = (Button) findViewById(R.id.add_b);
            b_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(activity, AjoutTache.class);
                    startActivityForResult(i, 1); //ouvre l'activité pour ajouter un nouvel élément dans la list
                }
            });
        }
     
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     
            if (requestCode == 1) {
                if(resultCode == RESULT_OK){ //un ajout a été validé en appuyant sur le bouton save
                    String result_title = data.getStringExtra("result_title"); //récupére le titre
                    String result_priority = data.getStringExtra("result_priority"); //récupére la priorité au format string dans mon cas
                    Tache mTache = new Tache(result_title, Integer.parseInt(result_priority)); //créer une nouvelle tache à ajouter dans la liste
                    mlist.add(mTache); //ajout dans la liste
                }
                if (resultCode == RESULT_CANCELED) { //ajout annulé via le bouton cancel
                    //écrire code si cancel
                }
            }
        }
    }
    xml associé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <LinearLayout 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:orientation="vertical"
        tools:context=".MainActivity">
     
        <ListView
            android:id="@+id/list_custom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
     
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add"
            android:textSize="16sp"
            android:id="@+id/add_b"/>
     
    </LinearLayout>
    seconde activité AjoutTache.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class AjoutTache extends ActionBarActivity {
        EditText ed_title;
        EditText ed_priority;
        Button b_save;
        Button b_cancel;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ajout_tache);
            ed_title = (EditText) findViewById(R.id.edit_title);
            ed_priority = (EditText) findViewById(R.id.edit_other);
            b_save = (Button) findViewById(R.id.button);
            b_save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent returnIntent = new Intent();
                    returnIntent.putExtra("result_title",ed_title.getText().toString());
                    returnIntent.putExtra("result_priority", ed_priority.getText().toString());
                    setResult(RESULT_OK, returnIntent); //on appuie sur save on retourne à l'activité principal le code RESULT_OK (1)
                                                        //avec un intent contenant les infos des EditText
                    finish();
                }
            });
            b_cancel = (Button) findViewById(R.id.cancel);
            b_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent returnIntent = new Intent();
                    setResult(RESULT_CANCELED, returnIntent); //l'ajout a été annulé, retourne le code RESULT_CANCEL (0)
                    finish();
                }
            });
        }
    }
    xml associé à cette seconde activité

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:gravity="center_horizontal">
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Titre"
                android:textSize="18sp"
                android:layout_marginEnd="10dp"/>
     
            <EditText
                android:id="@+id/edit_title"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="Entrez un titre"
                android:inputType="text" />
        </LinearLayout>
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Priorité"
                android:textSize="18sp"
                android:layout_marginEnd="10dp"/>
     
            <EditText
                android:id="@+id/edit_other"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:hint="Entrez un chiffre"
                android:inputType="text" />
        </LinearLayout>
     
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_horizontal">
     
            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Save" />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="cancel"
                android:id="@+id/cancel"/>
        </LinearLayout>
    </LinearLayout>
    CustomListAdapter.java (adapateur pour la listview)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    public class CustomListAdapter extends ArrayAdapter<Tache> {
        private Context context;
        private int resource;
        private ArrayList<Tache> list;
     
        public CustomListAdapter(Context context, int resource, ArrayList<Tache> list) {
            super(context, resource, list);
            this.context = context;
            this.resource = resource;
            this.list = list;
        }
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            RecordHolder holder;
     
            if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(resource, parent, false);
     
     
                holder = new RecordHolder();
                holder.tv_item = (TextView) row.findViewById(R.id.item);
                holder.tv_subitem = (TextView) row.findViewById(R.id.sub_item);
     
                row.setTag(holder);
            } else
                holder = (RecordHolder) row.getTag();
     
            Tache tache = list.get(position);
            holder.tv_item.setText(tache.getTitre());
            holder.tv_subitem.setText(tache.getPriorite()+"");
     
            return row;
        }
     
        static class RecordHolder {
            TextView tv_item;
            TextView tv_subitem;
        }
    }
    xml pour ajout dans la listview (item_row.xml)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
     
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
     
            <TextView
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/item" />
     
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/sub_item" />
     
        </LinearLayout>
    </RelativeLayout>
    J'ai mis quelques commentaire pour que tu puisses comprendre.

  7. #7
    Membre actif
    Homme Profil pro
    Inscrit en
    Février 2013
    Messages
    604
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 604
    Points : 206
    Points
    206
    Par défaut
    tout fonctionne comme je le souhaitais, merci de votre aide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [IIS 7] Problème de sécurité sur une application virtuelle
    Par yclaf dans le forum IIS
    Réponses: 0
    Dernier message: 25/07/2011, 21h04
  2. Réponses: 2
    Dernier message: 10/03/2011, 10h13
  3. Divers problèmes de performance sur une application Swing
    Par Julien Bodin dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 06/09/2010, 15h28
  4. Réponses: 1
    Dernier message: 11/04/2007, 09h03
  5. Réponses: 3
    Dernier message: 28/06/2006, 17h19

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo