Bonjour,
Je développe une application sur android pour appeller les services d'urgences. Je voudrais faire que sur la vue principale nous avons une liste de tout les pays et avec un barre (la barre recherche fonctionne).
J'ai voulu modifier le code et plus rien ne s'affiche dans la liste view.
Voilà mon code: (j'ai pris le code d'un site donc je ne l'ai pas encore adapté, niveau des noms etc)
Le code java de mon fragment Home
Le code Xml de ma page Home
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 package com.test.urgencecall; import android.app.Fragment; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.ListView; import java.util.ArrayList; public class HomeFragment extends Fragment { String[] listNames = { "Chine", "France", "Suisse", "Angleterre", "Amerique", }; ArrayList<Food> listFood; ListView lv; EditText search; public HomeFragment(){} @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { //construction de la vue View rootView = inflater.inflate(R.layout.fragment_home, container, false); lv = (ListView) rootView.findViewById(R.id.listFood); search = (EditText) rootView.findViewById(R.id.search); Drawable[] listPictures = { getResources().getDrawable(R.mipmap.ic_china), getResources().getDrawable(R.mipmap.ic_french), getResources().getDrawable(R.mipmap.ic_swiss), getResources().getDrawable(R.mipmap.ic_uk), getResources().getDrawable(R.mipmap.ic_us), }; listFood = new ArrayList<Food>(); for (int i = 0; i < listPictures.length; i++) { listFood.add(new Food(i + 1, listNames[i], listPictures[i])); } lv.setAdapter(new FoodListAdapter(getActivity().getApplicationContext(), listFood)); search.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub filtrer(); } }); //On retourne à l'utilisateur la vue return rootView; } public void filtrer() { // retourner la chaine saisie par l'utilisateur String name = search.getText().toString(); // créer une nouvelle liste qui va contenir la résultat à afficher ArrayList<Food> listFoodNew = new ArrayList<Food>(); for (Food food : listFood) { // si le nom du food commence par la chaine saisie , ajouter-le ! if (food.getName().toLowerCase().toString().startsWith(name)) { listFoodNew.add(food); } } // vider la liste lv.setAdapter(null); if (listFoodNew.size() == 0) { listFoodNew.add(new Food(100, "Pas d'élements.. réessayer !", getResources().getDrawable(R.drawable.ic_action), 0)); } // ajouter la nouvelle liste lv.setAdapter(new FoodListAdapter(getActivity().getApplicationContext(), listFoodNew)); } }
Le code java de Food :
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#ff0c2038"> <EditText android:id="@+id/search" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Cherhcer un pays" > </EditText> <ListView android:id="@+id/listFood" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@color/list_divider" > </ListView> </LinearLayout>
Maintenant de FoodListAdapter
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 package com.test.urgencecall; import android.graphics.drawable.Drawable; /** * Created by vuffrayju on 28.04.2015. */ public class Food { int id; String name; Drawable picture; float price; public Food(int id, String name, Drawable picture, float price) { super(); this.id = id; this.name = name; this.picture = picture; } public Food(int i, String listName, Drawable listPicture) { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Drawable getPicture() { return picture; } public void setPicture(Drawable picture) { this.picture = picture; } @Override public String toString() { return "Food [id=" + id + ", name=" + name + ", picture=" + picture +"]"; } }
Maintenant le code XML de FoodRow
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
80
81
82
83
84
85
86
87
88 package com.test.urgencecall; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; /** * Created by vuffrayju on 28.04.2015. */ public class FoodListAdapter extends BaseAdapter { private List<Food> listFood = null; LayoutInflater layoutInflater; Context context; private int lastPosition = -1; // constructeur public FoodListAdapter(Context context, List<Food> listFood) { this.listFood = listFood; layoutInflater = LayoutInflater.from(context); this.listFood = listFood; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return listFood.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return listFood.get(position); } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } static class ViewHolder { TextView nomView; ImageView pictureView; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.food_row, null); holder = new ViewHolder(); // initialisation des vues holder.nomView = (TextView) convertView.findViewById(R.id.name); holder.pictureView = (ImageView) convertView.findViewById(R.id.picture); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // affchier les données convenablement dans leurs positions holder.nomView.setText(listFood.get(position).getName()); holder.pictureView.setBackgroundDrawable(listFood.get(position) .getPicture()); // changer R.anim.ton_effet Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.up_from_bottom); convertView.startAnimation(animation); lastPosition = position; return convertView; } }
Voilà un site pour voir le fonctionnement de mon application : https://appetize.io/app/85pbz7aqkmcxkent3rgmzcfcbm
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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dip" android:background="#ff0b1e34"> <LinearLayout android:id="@+id/thumbnail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginRight="5dip" android:padding="3dip" > <ImageView android:id="@+id/picture" android:layout_width="50dip" android:layout_height="50dip" /> </LinearLayout> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" android:textColor="#FFFF" android:textSize="20dip" android:typeface="sans" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Partager