Bonjour,
Mon appli n`affiche pas le ExpandListview.
J`ai modifie la base adapteur pour HashMap comme suivant, mais rien ne s`affiche.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public class DataItem { public String Name; public String CreationTimeDescription; public String Description; public DataItem(String name, String creationTimeDescription, String description) { this.Description = description; this.Name = name; this.CreationTimeDescription = creationTimeDescription; } }
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 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; List<String> listDataHeader = new ArrayList<String>(); private HashMap<String, DataItem> listDataChild; public ExpandableListAdapter(Context context, ArrayList<DataItem> filteredList) { this.context = context; listDataChild = new HashMap<String, DataItem>(); for (DataItem data : filteredList) { this.listDataChild.put(data.Name, data); } for (DataItem data : filteredList) { listDataHeader.add(data.Name); } } @Override public Object getChild(int groupPosition, int childPosition) { return listDataChild.get(this.listDataHeader.get(groupPosition)); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (String) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, null); } TextView tv = (TextView) convertView.findViewById(R.id.lblListItem); tv.setText(childText); return convertView; } @Override public int getChildrenCount(int groupPosition) { return this.listDataChild.size(); } @Override public Object getGroup(int groupPosition) { return this.listDataHeader.get(groupPosition); } @Override public int getGroupCount() { return this.listDataHeader.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub String headertext = (String) getGroup(groupPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_group, null); } TextView txtGroup = (TextView) convertView.findViewById(R.id.lblListHeader); txtGroup.setText(headertext); return convertView; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return false; } }
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 public class MainActivity extends Activity { ExpandableListView ex_listview; ExpandableListAdapter listadapter; List<String> listDataHeader; HashMap<String, DataItem> listDataChild; ArrayList<DataItem> values = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ex_listview = (ExpandableListView) findViewById(R.id.ex_list); setdata(); listadapter = new Test.hashlistviewapplication.ExpandableListAdapter(this, values); ex_listview.setAdapter(listadapter); ex_listview.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { // TODO Auto-generated method stub return false; } }); ex_listview.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " Expanded", Toast.LENGTH_SHORT).show(); } }); ex_listview.setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), listDataHeader.get(groupPosition) + " Collapsed", Toast.LENGTH_SHORT).show(); } }); } private void setdata() { DataItem val = new DataItem("WORLD", "PALCE", "1212", "JHJHJ", "TYTYT", "HGHGHH"); values.add(val); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); return true; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <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" tools:context=".MainActivity" > <ExpandableListView android:id="@+id/ex_list" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ExpandableListView>
list_group.xml
list_item.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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="8dp" android:background="#000000"> <TextView android:id="@+id/lblListHeader" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" android:textSize="17dp" android:textColor="#f9f93d" />
Mecrci d`avance!
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 <?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="55dip" android:orientation="vertical" > <TextView android:id="@+id/lblListItem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="17dip" android:paddingTop="5dp" android:paddingBottom="5dp" android:textColor="#000000" android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> </LinearLayout>
Partager