my expandable list view doesn't seem to be in right order.
Here is my dataprovider for it:
public class DataProvider {
public static HashMap<String, List<String>> getInfo() {
HashMap<String, List<String>> WorkoutDetails = new HashMap<String, List<String>>();
List<String> Workout_29 = new ArrayList<String>();
Workout_29.add("Week One");
Workout_29.add("Week Two");
Workout_29.add("Week Three");
Workout_29.add("Week Four");
List<String> Workout_30 = new ArrayList<String>();
Workout_30.add("Week One");
Workout_30.add("Week Two");
Workout_30.add("Week Three");
Workout_30.add("Week Four");
List<String> Workout_31 = new ArrayList<String>();
Workout_31.add("Week One");
Workout_31.add("Week Two");
Workout_31.add("Week Three");
Workout_31.add("Week Four");
List<String> Workout_32 = new ArrayList<String>();
Workout_32.add("Week One");
Workout_32.add("Week Two");
Workout_32.add("Week Three");
Workout_32.add("Week Four");
List<String> Workout_37 = new ArrayList<String>();
Workout_37.add("Week One");
Workout_37.add("Week Two");
Workout_37.add("Week Three");
Workout_37.add("Week Four");
WorkoutDetails.put("Workout 29", Workout_29);
WorkoutDetails.put("Workout 30", Workout_30);
WorkoutDetails.put("Workout 31", Workout_31);
WorkoutDetails.put("Workout 32", Workout_32);
WorkoutDetails.put("Workout 37", Workout_37);
return WorkoutDetails;
}
}
Here is where it is created:
public class MyWorkout extends BaseActivity{
ExpandableListView expandableListView;
HashMap<String, List<String>> Workouts_details;
List<String> Workout_list;
WorkoutsAdapter mAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");
expandableListView = (ExpandableListView) findViewById(R.id.listViewWorkouts);
Workouts_details = DataProvider.getInfo();
Workout_list = new ArrayList<>(Workouts_details.keySet());
mAdapter = new WorkoutsAdapter(this, Workouts_details, Workout_list);
expandableListView.setAdapter(mAdapter);
As you can see it should be 29,30,31,32,37.. but in reality it is 29,30,32,37,31... Any ideas why? Thanks!
This is most likely because HashMap provides no guarantees about the ordering of its keys in the returned keyset. My guess is that if you subsequently sort the list that you build here, you would get your desired ordering:
Workout_list = new ArrayList<>(Workouts_details.keySet());
// Now sort Workout_list...
// As it is simply a list of strings, we can sort it according to its natural ordering (lexicographical) to get your desired result like so:
java.util.Collections.sort(Workout_list);
You can change HashMap to LinkedHashmap. Like that
LinkedHashMap<String, List<String>> WorkoutDetails = new LinkedHashMap<String, List<String>>();
Related
Please look into my requirement and suggest a solution. Okay here is my requirement, I have an expandable-list-view with one group item and one child item which contain a button. On the click of the child button, I want to remove the complete group. How can I do this?
here is my activity is as follows
public class TaskActivity extends AppCompatActivity {
ExpandableListView expandableListView;
ExpandableListViewAdapter expandableListViewAdapter;
List<String> Tasklist;
HashMap<String, List<String>> childitemslist;
Window window;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task);
if(Build.VERSION.SDK_INT>=21){
window=this.getWindow();
window.setStatusBarColor(getColor(R.color.primaryTextColor));
}
Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Pending Tasks");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
expandableListView = findViewById(R.id.expandable_listview);
showList();
expandableListViewAdapter = new ExpandableListViewAdapter(TaskActivity.this, Tasklist, childitemslist);
expandableListView.setAdapter(expandableListViewAdapter);
}
private void showList() {
Tasklist = new ArrayList<String>();
childitemslist = new HashMap<String, List<String>>();
Tasklist.add("new task-1");
Tasklist.add("new task-2");
Tasklist.add("new task-3");
Tasklist.add("new task-4");
Tasklist.add("new task-5");
Tasklist.add("new task-6");
Tasklist.add("new task-7");
List<String> description1 = new ArrayList<String>();
description1.add("Description-1 should be done in this task before 29th should be done in this task before 29th should be done in this task before 29th");
List<String> description2 = new ArrayList<String>();
description2.add("Description-2");
List<String> description3 = new ArrayList<String>();
description3.add("Description-3");
List<String> description4 = new ArrayList<String>();
description4.add("Description-4");
List<String> description5 = new ArrayList<String>();
description5.add("Description-5");
List<String> description6 = new ArrayList<String>();
description6.add("Description-6");
List<String> description7 = new ArrayList<String>();
description7.add("Description-7");
childitemslist.put(Tasklist.get(0), description1);
childitemslist.put(Tasklist.get(1), description2);
childitemslist.put(Tasklist.get(2), description3);
childitemslist.put(Tasklist.get(3), description4);
childitemslist.put(Tasklist.get(4), description5);
childitemslist.put(Tasklist.get(5), description6);
childitemslist.put(Tasklist.get(6), description7);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
I've created ExpandableListView that is shown first when you run the app.
But now I have created another activity with ImageView to be the main page of the app. I don't want the ExpandableListView to be the first shown - only to show after click on the ImageView. How I can do that?
Here is the code.
public class MainActivity extends AppCompatActivity {
ExpandableListViewAdapter listViewAdapter;
ExpandableListView expandableListView;
List<String> chapterList;
HashMap<String, List<String>> topicList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.eListView);
showList();
listViewAdapter = new ExpandableListViewAdapter(this, chapterList, topicList);
expandableListView.setAdapter(listViewAdapter);
}
private void showList() {
chapterList = new ArrayList<String>();
topicList = new HashMap<String, List<String>>();
chapterList.add("Chapter 1");
chapterList.add("Chapter 2");
chapterList.add("Chapter 3");
chapterList.add("Chapter 4");
chapterList.add("Chapter 5");
List<String> topic1 = new ArrayList<>();
topic1.add("Topic 1");
topic1.add("Topic 2");
topic1.add("Topic 3");
List<String> topic2 = new ArrayList<>();
topic2.add("Topic 1");
topic2.add("Topic 2");
topic2.add("Topic 3");
List<String> topic3 = new ArrayList<>();
topic3.add("Topic 1");
topic3.add("Topic 2");
topic3.add("Topic 3");
List<String> topic4 = new ArrayList<>();
topic4.add("Topic 1");
topic4.add("Topic 2");
topic4.add("Topic 3");
List<String> topic5 = new ArrayList<>();
topic5.add("Topic 1");
topic5.add("Topic 2");
topic5.add("Topic 3");
topicList.put(chapterList.get(0),topic1);
topicList.put(chapterList.get(1),topic2);
topicList.put(chapterList.get(2),topic3);
topicList.put(chapterList.get(3),topic4);
topicList.put(chapterList.get(4),topic5);
}
}
You need to remove showList() from the onCreate-Method. Instead, find your ImageView by id using ImageView yourImageView = findViewById(R.id.your_image_view). And then add an OnClickListener to yourImageView that calls the showList()-method like so:
yourImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showList();
}
});
I have a listvew and I add subitem into the listview like this code
public class MyCustomListView extends Activity {
/** Called when the activity is first created. */
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
lv=(ListView) this.findViewById(R.id.lvvv);
SimpleAdapter adapter = new SimpleAdapter(this,list, R.layout.custom_row_view,new String[] {"pen","color"},
new int[] {R.id.text1, R.id.text3}
);
populateList();
lv.setAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("pen","MONT Blanc");
temp.put("color", "Black");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("pen","Gucci");
temp1.put("color", "Red");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("pen","Parker");
temp2.put("color", "Blue");
}
}
but in the color key, if I have a lot of colors for a pen, and I want to arrange the colors into a list and this color list is under the pen.
ex:
-pen: Parker.
+color: blue.
+color: red.
how should I do that? please help me!
thank alot.
You need to use an ExpanadableListView. Check out this -
ExpanadaleListView Tutorial
After lot of research over internet i could not find a answer how to enable a phone call in a list view.
here is my sample code:
Suppose i have a list of 50 hospital with there phone contact detail
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bus_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
How to enable phone call to all listview entry based on individual Phone number detail (text3) of a hospital.
add this method in your activity:
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.textView1,R.id.textView2, R.id.textView3}
);
populateList();
setListAdapter(adapter);
}
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l,v,position,id);
if(position>=0 && position<list.size()) {
HashMap<String, String> tmp = list.get(position);
if(tmp.containsKey("phone")) {
String tel = tmp.get("phone");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+tel));
startActivity(callIntent);
}
}
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
and add
<uses-permission android:name="android.permission.CALL_PHONE" />
in your manifest
Attention This code does not check if the number is valid.
I'm writing simple filter in Android and want to use ExpandableListAdapter with check boxes.
I have no problem with creating list or checking checkbox. but I really don't know how to remember choices. After closing group and opening again or when I try to open different group checkboxes are changes.
I tried to reading on the net about that but I didn`t find how to solve my problem.
Here is my code:
public class TasksFilterDialog extends Dialog{
private static final String TAG = "Filter Dialog";
private static final String ChildID = "ChildID";
private static final String GroupID = "GroupID";
private boolean[] statusCheck;
ExpandableListView list;
SimpleExpandableListAdapter listAdapter;
public TasksFilterDialog(Context context) {
super(context);
statusCheck = new boolean[Tasks.Status.values().length];
for(int i=0; i<statusCheck.length; i++)
statusCheck[i]=true;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "Creating dialog");
this.setContentView(R.layout.tasks_filter);
list = (ExpandableListView)findViewById(R.id.filter_list);
setListAdapter(); // Setting Adapter Values to use on list
list.setAdapter(listAdapter); // Setting creating adapter to list
setOnChildClickListeners();
Log.d(TAG, "Creating dialog successfull");
}
private void setListAdapter() {
Log.d(TAG, "Setting list adapter");
listAdapter = new SimpleExpandableListAdapter(this.getContext(),
getGroups(),
R.layout.tasks_filter_groups_layout,
new String[] {GroupID},
new int[] {R.id.filter_group_text},
getChilds(),
R.layout.tasks_filter_simple_element,
new String[] {ChildID},
new int[] {R.id.filter_simple_element_text});
Log.d(TAG, "Setting list adapter successfull");
}
private List<HashMap<String, String>> getGroups() {
Log.d(TAG, "Adding groups values");
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> statusMap = new HashMap<String, String>();
statusMap.put(GroupID, "Status");
list.add(statusMap);
HashMap<String, String> usersMap = new HashMap<String, String>();
usersMap.put(GroupID, "Users");
list.add(usersMap);
Log.d(TAG, "Adding groups values successfull");
return list;
}
private List<List<HashMap<String, String>>> getChilds() {
Log.d(TAG, "Adding childs values");
List<List<HashMap<String, String>>> list = new ArrayList<List<HashMap<String, String>>>();
List<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
Tasks.Status status[] = Tasks.Status.values();
for(int i=0; i<status.length; i++) {
Log.d(TAG, "Adding child" + status[i].toString());
map = new HashMap<String, String>();
map.put(ChildID, status[i].toString());
secList.add(map);
}
list.add(secList);
list.add(secList);
Log.d(TAG, "Adding childs values succesfull");
return list;
}
private void setOnChildClickListeners() {
list.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView list, View v,
int group, int id, long arg4) {
CheckBox cb = (CheckBox) v.findViewById(R.id.filter_simple_element_check);
cb.toggle();
switch(group){
case 0:
if(statusCheck[id])
statusCheck[id]=false;
else
statusCheck[id]=true;
break;
case 1:
//TODO After getting Tasks function
}
return false;
}
});
}
}
It depends on how much you want to engineer it. A Map> would work. The Map takes a position and gives you a set of checked children. To determine if a child is checked or not, call Map.get() on group, and then see if the set contains the id of the child.
If you want to see if the second child of the third group is checked, you could do:
boolean isChecked = yourMap.get(3).contains(2);
To set a child checked:
yourMap.get(groupNum).add(childNum);
To set a child unchecked:
yourMap.get(groupNum).remove(childNum);
You would need to initialize the map to contain empty sets for every group to avoid NPEs, but thats easy to do when you first create the map. I'm sure you could come up with a cleaner way to do this, but its simple and it would work.