Salut!
Alors voilà, j'aimerais créer une application ou dans le main.xml on affiche une liste, et lorsque l'on clique sur l'un des objets de la liste on lance une nouvelle activity qui va afficher des détails sur l'objet choisi.
J'ai regardé plusieurs forum pour me faire une idée de la marche à suivre et voici ce que j'ai fait :
Classe principale :
Comme je souhaite passer par un controlleur pour changer d'activité il y a une classe controller :
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 public class ManagementAlarm extends Activity implements AlarmeAdapterListener{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<ListAlarme> listP = ListAlarme.getAListOfPersonne(); AlarmeAdapter adapter = new AlarmeAdapter(this, listP); adapter.addListener(this); ListView list = (ListView)findViewById(R.id.listalarme); list.setAdapter(adapter); Controller.managementAlarm = this; Controller.ecranActif = this; Intent intent1 = new Intent(this,DetailAlarm.class); Controller.intent1 = intent1; // this.startActivity(intent1); } public void onClick(ListAlarme item, int position) { Controller.ShowDetailAlarm(); }
puis deux autres classes : AlarmeAdapter et DetailAlarm
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 public class Controller { static public ManagementAlarm managementAlarm; static public DetailAlarm detailAlarm; static public Activity ecranActif; static public Intent intent1; public static void ShowDetailAlarm(){ Controller.ecranActif.startActivityIfNeeded(intent1, 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
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 public class AlarmeAdapter extends BaseAdapter{ private List<ListAlarme> mListA; private Context mContext; private LayoutInflater mInflater; public AlarmeAdapter(Context context, List<ListAlarme> aListA) { mContext = context; mListA = aListA; mInflater = LayoutInflater.from(mContext); } public int getCount() { return mListA.size(); } public Object getItem(int position) { return mListA.get(position); } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { LinearLayout layoutItem; if (convertView == null) { layoutItem = (LinearLayout) mInflater.inflate(R.layout.alarme_layout, parent, false); } else { layoutItem = (LinearLayout) convertView; } TextView alarmeID = (TextView)layoutItem.findViewById(R.id.alarme_id); TextView alarmeType = (TextView)layoutItem.findViewById(R.id.alarme_type); alarmeID.setText(mListA.get(position).idAlarme); alarmeType.setText(mListA.get(position).typeAlarme); if (mListA.get(position).genre == ListAlarme.grave) { layoutItem.setBackgroundColor(Color.RED); } else { layoutItem.setBackgroundColor(Color.YELLOW); } layoutItem.setTag(position); layoutItem.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Integer position = (Integer)v.getTag(); sendListener(mListA.get(position), position); } }); return layoutItem; } private ArrayList<AlarmeAdapterListener> myListListener = new ArrayList<AlarmeAdapterListener>(); public void addListener(AlarmeAdapterListener aListener) { myListListener.add(aListener); } private void sendListener(ListAlarme item, int position) { for(int i = myListListener.size()-1; i >= 0; i--) { myListListener.get(i).onClick(item, position); } } /** * Interface pour écouter les évènements sur le nom d'une personne */ public interface AlarmeAdapterListener { public void onClick(ListAlarme item, int position); } }Mon problème est le suivant : Lorsque je lance l'application j'affiche bien ma liste d'alarme (que j'ai créer dans la class ListAlarm mais je ne vous l'ai pas copié) cependant dés que je clique sur un des objets de ma liste, je dois forcer la femeture de l'application ... Des idées de solutions? Merci
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 public class DetailAlarm extends Activity{ public Controller controller; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.detailalarm); Controller.detailAlarm = this; Controller.ecranActif = this; TextView textalarm = (TextView) findViewById(R.id.textview); textalarm.setText(R.id.alarme_id); } }
Partager