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; } }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 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); } }; }
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
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>
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
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>
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>
Partager