How to dynamically populate ExpandableListView through user input? - android

I'm building a GPA tracker app and I find that an ExpandableListView is the most appropriate thing to use. The function is this:
The user clicks on a FAB button and adds a Semester (group/header).
The user clicks on the generated Semester (group/header) and adds a course.
I can add the groups but I couldn't figure out how to dynamically add children for those groups. Basically, I want to get the ID of the clicked group, start an alertDialog to have the user input the course information and add that information to a List.
I tried to do that with the setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()) method but now I can't expand the group. This wasn't my final implementation anyways. I really just wanted to get this logic to work.
These are the Lists and HashMaps that contain my Semester and Course information:
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader = new ArrayList<>();
HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
List<String>listCourses = new ArrayList<>();
private String semester, course;
This is where I am adding my Semesters (group/header)
private void addSemester() {
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.parent_dialog_layout, null);
final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create A Semester");
builder.setView(dialogView);
builder.setCancelable(false);
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
semester = et_semester.getText().toString();
listDataHeader.add(semester);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
listAdapter.notifyDataSetChanged();
builder.show();
}
And in my onCreate(), I'm calling my setOnGroupClickListener method to add the children:
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, final int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.child_dialog_layout, null);
final EditText et_course = (EditText)dialogView.findViewById(R.id.et_course);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Create A Course");
builder.setView(dialogView);
builder.setCancelable(false);
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
course = et_course.getText().toString();
listDataChild.put(listDataHeader.get(groupPosition), listCourses);
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
listAdapter.notifyDataSetChanged();
builder.show();
return true;
}
});
listAdapter.notifyDataSetChanged();
}
I want to access the clicked group's ID and then add my children to that group.

call notifyDataSetChanged(); inside your Dialog's interface OnClickListener:
builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
course = et_course.getText().toString();
listDataChild.put(listDataHeader.get(groupPosition), listCourses);
notifyDataSetChanged();
}
});

Related

Remove item list from DB MSSQL using ListView.setOnItemLongClickListener Android

I have created a database which is displayed in a ListView using an SimpleAdapter. Now, I want to show a layout which contain (edit & delete ), so when user longClick on an item of the list it will get a dialog to choose if its want to delete or to edit on this item. Now, I'm focusing on delete statement and
I have tried with this code, but it does not work for me, the program always force close when I use this code.
Thanks
#Override
protected void onCreate(Bundle savedInstanceStates){
super.onCreate(savedInstanceStates);
setContentView(R.layout.activity_user_lstview);
userDataListView = (ListView) findViewById(R.id.ListViewUser);
List<Map<String, String>> data = new ArrayList<Map<String, String>>(
String[] from = {"tvId", "tvUsr", "tvPass", "tvDept","tvAuth"};
int[] to = {R.id.tvId, R.id.tvUsr, R.id.tvPass, R.id.tvDept, R.id.tvAuth};
adapter = new SimpleAdapter(UserListView.this, data, R.layout.userlisttemplate, from, to);
userDataListView.setAdapter(adapter);
userDataListView.setLongClickable(true);
userDataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),"Clicked item : " +adapter.getItem(position), Toast.LENGTH_LONG).show();
}
});
userDataListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
final int which_item = position;
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(UserListView.this);
dialogBuilder.setIcon(android.R.drawable.ic_dialog_alert);
dialogBuilder.setTitle("Confirmation");
dialogBuilder.setMessage("Choose an option edit or delete data ?");
dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Edit
­­
dialogInterface.dismiss();
}
});
dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
// Delete
data.remove(which_item);
adapter.notifyDataSetChanged();
Toast.makeText(UserListView.this, "Data has been removed", Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
dialogBuilder.create().show();
return true;
}
});
}

Proceed to next activity through radio buttons on alert dialog

I have listview ,on click of each list item, it must pop up a alert with radio buttons. Selecting a radio button option and then clicking "ok" button on alert dialog , I must be able to proceed to next activity. (PS i dont want to use positive , negative button ).
Below is my code, listview is working fine , alert dialog pops up and on selecting yes or no , Toast shows .But upon yes it isn't proceeding to next activity. Please help!!
listview = (ListView) findViewById(R.id.mylistview);
final String[] items = new String[]{"IOS", "ANDROID", "WINDOWS"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
You have problem in below code snippet, As you covert whole string array into string , but you need to get one item at time.
switch(items1.toString())
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
Please replace this with
String selection;
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int itemposition = position;
String itemvalue = (String) listview.getItemAtPosition(position);
final CharSequence[] items1 = {"yes", "no"};
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("slection confirmation");
builder.setSingleChoiceItems(items1, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selection = items1[which]
Toast.makeText(getApplicationContext(), items1[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch(selection)
{
case("yes"):
Intent myint=new Intent(MainActivity.this,secondpage.class);
myint.putExtra("act1","");
startActivity(myint);
break;
case("no"):
dialog.cancel();
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});

DialogInterface with a ListView

My problem is that I am trying to open up a dialogInterface with a ListView that has a row with 1 TextView and a CheckBox. The problem I have is that when the checkBox is clicked, lets say on item 2, item 6 will also be checked, and item 10, and item 14, and all the way when all I want is item 2 to be picked.
What I have tried:
//setup a list adapter and then set that on the list
ListAdapter listAdapter = new ArrayAdapter(PendingFriendsActivity.this, R.layout.activity_contact_list_item, R.id.contactNameTextView, ContactList);
AlertDialog.Builder builderSingle = new AlertDialog.Builder(PendingFriendsActivity.this);
DialogInterface.OnClickListener buttonClicked;
buttonClicked = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
for(int i = 0; i < sendContactList.size();i++)
{
String phone, name;
name = sendContactList.get(i).get(0);
phone = sendContactList.get(i).get(1);
sendSMS(phone);
}
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
builderSingle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
ArrayList<String> contact = new ArrayList<String>();
final CheckBox contactCheckBox = (CheckBox) vwParentRow.findViewById(R.id.sendCheckBox);
TextView contactTextView = (TextView) vwParentRow.findViewById(R.id.contactNameTextView);
Toast.makeText(PendingFriendsActivity.this, contactTextView.getText().toString(),Toast.LENGTH_LONG).show();
if(contactCheckBox.isChecked())
{
contact.add(fullContacts.get(i).get(0));
contact.add(fullContacts.get(i).get(1));
sendContactList.add(contact);
} else {
sendContactList.remove(i);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
builderSingle.setCancelable(false);
builderSingle.setTitle("Friend Invite");
builderSingle.setNegativeButton("Cancel", buttonClicked);
builderSingle.setPositiveButton("Send Invites", buttonClicked);
builderSingle.setAdapter(listAdapter, buttonClicked);
builderSingle.show();
Try something like this :
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// make a list to hold state of every color
for (int i = 0; i < colors.length; i++) {
ColorVO colorVO = new ColorVO();
colorVO.setName(colors[i]);
colorVO.setSelected(checkedColors[i]);
colorList.add(colorVO);
}
// Do something here to pass only arraylist on this both arrays ('colors' & 'checkedColors')
builder.setMultiChoiceItems(colors, checkedColors, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// set state to vo in list
colorList.get(which).setSelected(isChecked);
Toast.makeText(getApplicationContext(),
colorList.get(which).getName() + " " + isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false);
builder.setTitle("Your preferred colors?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
txtSelected.setText("Your preferred colors..... \n");
// save state of selected vos
ArrayList<ColorVO> selectedList = new ArrayList<>();
for (int i = 0; i < colorList.size(); i++) {
ColorVO colorVO = colorList.get(i);
colors[i] = colorVO.getName();
checkedColors[i] = colorVO.isSelected();
if (colorVO.isSelected()) {
selectedList.add(colorVO);
}
}
for (int i = 0; i < selectedList.size(); i++) {
// if element is last then not attach comma or attach it
if (i != selectedList.size() - 1)
txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName() + " ,");
else
txtSelected.setText(txtSelected.getText() + selectedList.get(i).getName());
}
colorList.clear();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// make sure to clear list that duplication dont formed here
colorList.clear();
}
});
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// make sure to clear list that duplication dont formed here
colorList.clear();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Hope this can help.!

Spinner from editext android

My Scenario:
When I click the top (+)icon there is a dialog displayed with editext and If I enter some text and click ok button the text should be added to my spinner which I am unable to do it.
Here is what I mean to say:
This is what I have done:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Spinner element
listsp = (Spinner) findViewById(R.id.listspinner);
listtext = (EditText) findViewById(R.id.list_text);
list = new ArrayList<String>();
list.add(listtext.getText().toString());
listadapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Try to update the adapter outside the onClick :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Whatever else
listsp = (Spinner) findViewById(R.id.listspinned);
list = new ArrayList<String>();
listadapter = new MyArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, list);
listadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listsp.setAdapter(adapter);
}
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listtext = (EditText) findViewById(R.id.list_text);
updateAdapter(listtext.getText().toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
protected void updateAdapter(String input) {
list.add(input);
listadapter.notifyDataSetChanged();
}
EDIT : Here's how to implement your custom adapter (I made it private so it'd use the same dataList. Therefore, you don't need to call any updateData() function, just to notify the adapter that the data has changed with notifyDataSetChanged()) :
private class MyArrayAdapter extends BaseAdapter implements SpinnerAdapter {
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(list.get(position).getName());
return text;
}
}

DialogFragment with extra space below ListView

As you can see, below the bottom list element in my ListView, there is excess space I can't seem to be rid of. I've tried Relative and Linearlayout, both look like this. Here's the code:
public class ChooseDialog extends DialogFragment implements
DialogInterface.OnClickListener {
String URLhome;
String Title;
String type;
/* public static ChooseDialog newInstance() {
ChooseDialog dialog = new ChooseDialog();
Log.v("a", "shit runs");
Bundle bundle = new Bundle();
dialog.setArguments(bundle);
return dialog;
}*/
public ChooseDialog(String type) {
this.type = type;
}
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(type);
builder.setNegativeButton("Cancel", this);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogLayout);
final String[] items = {"Red", "Green", "Blue" };
builder.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.v("touched: ", items[which].toString());
}}
);
return builder.create();
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
And the code that launches the dialog:
public OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
showNationalityDialog();
}
};
private void showNationalityDialog() {
FragmentManager fm = getSupportFragmentManager();
ChooseDialog nationalityDialog = new ChooseDialog("Nationality");
nationalityDialog.show(fm, "fragment_edit_name");
}
I know this question never drew much attention, but I finally solved the problem.
By using the listview that I created in XML rather than setting the builder's adapter, I managed to get rid of all the excess space.
Here's what the new code looks like:
switch (editText.getId()) {
case (0) :
ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist,
activity.getResources().getStringArray(R.array.ageArray)));
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
editText.setText(activity.getResources().getStringArray(R.array.ageArray)[arg2]);
dismiss();
}
});
builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) :
new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);
builder.setNegativeButton("Cancel", this);
builder.setView(dialogLayout);
return builder.create();
If you are setting a custom view on the alert dialog (via setView()) that ONLY has a ListView then you don't need to use a custom view. The builder will automatically add a ListView into the view if set adapter is called.
The extra space at the end of the list view is probably your custom view with no content.
For example:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AccountChooserListAdapter adapter = new AccountChooserListAdapter(getActivity(), R.layout.choose_account_list_item,
accountMetadataFactory.getAccountsAsList());
return new AlertDialog.Builder(getActivity())
.setCancelable(true)
.setTitle(getActivity().getString(R.string.title_add_account))
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
listener.onAddAccount(which);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}

Categories

Resources