i am trying to make an alertdialog.builder with a spinner inside it. the spinner item is originally populated from the database. here is part of my code:
update.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(update_layout);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
List<String> arrayspin=new ArrayList<String>();
Cursor csr1 = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
int count = csr1.getCount();
csr1.moveToFirst();
for (int m=0; m>count; m++){
if (csr1.getString(csr1.getColumnIndex("name"))!="android_metadata")
{
String cont = csr1.getString(csr1.getColumnIndex("name"));
arrayspin.add(cont);
}
csr1.moveToNext();
}
ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,arrayspin);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
table2 = spinner.getSelectedItem().toString();
editText2 = (EditText) update_layout.findViewById(R.id.editText2);
new Updating().execute();
}
});
builder.setNegativeButton("back",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
it gave me no error, but in the app, the spinner is showing no items. then i try to modified the code, just to test the spinner, into something like this:
update.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(update_layout);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
String[] items = { "this", "is", "a", "really","really2", "really3",
"really4", "really5", "silly", "list" };
ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,items);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
table2 = spinner.getSelectedItem().toString();
editText2 = (EditText) update_layout.findViewById(R.id.editText2);
new Updating().execute();
}
});
builder.setNegativeButton("back",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}});
AlertDialog alert = builder.create();
alert.show();
}});
but the spinner still not showing any items. what do i do wrong?
You are populating your spinner on click of Alert Dialog positive button. You should move the code outside of the onClickListener
final View update_layout = getLayoutInflater().inflate(
R.layout.update_layout, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
Spinner spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
String[] items = { "this", "is", "a", "really", "really2",
"really3", "really4", "really5", "silly", "list" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(HomeScreen.this,
android.R.layout.simple_spinner_dropdown_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
builder.setView(update_layout);
AlertDialog alert = builder.create();
alert.show();
Related
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();
}
});
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();
}
});
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;
}
}
Why dialog.dismiss() doesn't work after click on item inside the AlertDialog ?
I have also tried alert.dismiss but i get the error dialog cannot be resolved. How can i solve that?
Here's the dialog
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
builder.setTitle("List");
lv = (ListView) convertView.findViewById(R.id.listView1);
if (prefs.contains("Name") || prefs.contains("NameTwoo"))
{
String[] values = new String[] {name,nameTwoo};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg) {
String selected;
selected = lv.getItemAtPosition(position).toString();
if(selected.equalsIgnoreCase(name))
{
mapView.getMapViewPosition().setCenter(myMarkerGeopoint);
dialog.dismiss();
}
}
});
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // I get error!
}
});
AlertDialog alert = builder.create();
alert.show();
}
Declare the AlertDialog before build it and use it to dismiss.
AlertDialog alert;
protected void myMarkersDialog() {
final String name = prefs.getString("Name", "");
String nameTwoo = prefs.getString("NameTwoo", "");
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.my_markers_listview, null);
builder.setView(convertView);
...
builder.setNeutralButton(getResources().getString(R.string.no_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert = builder.create();
alert.show();
}
I am creating a chat app so I need to implement the broadcast feature, I have placed an AlertDialog within a button of another AlertDialog - so that when the user wants to add contact(s) or select all the contact names from the simple cursor adapter they are returned.
My problem is that when the contacts button is clicked the ListView shows the contact names correctly but when I click both the "Done" or "Select All" buttons, they produce a NullPointerException error instead of the checked names. Please help - thanks in advance.
Code is below for the callBroadcast() function
void callBroadcast()
{
// get broadcast.xml view
LayoutInflater li = LayoutInflater.from(context);
View broadPop = li.inflate(R.layout.broadcast, null);
contacts = (Button) broadPop.findViewById(R.id.contacts);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(broadPop);
final EditText userInput = (EditText) broadPop.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
contacts.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
LayoutInflater li = LayoutInflater.from(context);
final View contactList = li.inflate(R.layout.be_sponge_contacts, null);
SimpleCursorAdapter listAdapter;
ListView contact_list = (ListView) findViewById( R.id.contactList );
String[] selectionArgs = null;
String[] projection = new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+" COLLATE LOCALIZED ASC";
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP+"='"+("1")+"'";
ContentResolver cr = getContentResolver();
final Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[] {R.id.rowTextViewChecked};
listAdapter = new SimpleCursorAdapter(Chats.this, R.layout.simplerow_checked, cur, from, to);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setCancelable(false)
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
}
}
})
.setNeutralButton("Select All", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
selected = (CheckBox) contactList.findViewById(R.id.rowTextViewChecked);
//setting all to be checked
selected.setChecked(true);
if (selected.isChecked())
{
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(Chats.this, name+" was chosen", Toast.LENGTH_SHORT).show();
selected.setChecked(true);
}
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// creating an alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// showing
alertDialog.show();
}
});
test = (TextView) broadPop.findViewById(R.id.namesChosen);
}