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);
}
Related
In my android app, I need to generate one Alertdialog with a list of Company name. For example Company 1, Company 2, Company 3. Now if the user cllick company 1, he will get second alertdialog which will show some actions. Like Phone Call, Email, etc. Now I have implemented this two alertdialog in my code. But what I want to do, that for each company there should be different Phone number and email adress. So Far I have tried with same number with all the company. But in real in real if user click company 1, he will get the second alert list of action with phone call, email. if he clicks phone option he will see the phone number company 1, if he clicks company 2, he will get alertoption with phone number of company 2. But I am very new in developing area. I know there is something with Mapping topic, by which I can do it easily but I am not getting actually how to proceed with it. My code is like this
public List<CompanyDetail> setCompanydata(){
int n = 3;
private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>(); //modifier private is not allowed here
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>(); //modifier private is not allowed here
for(int i=0;i<n;i++){
private CompanyDetail comD= new CompanyDetail(); //modifier private is not allowed here
comD.setcompanyPhoneNo(companyPhoneno); //cannot resolve problem companyPhone
comD.setcompanyEmail(compnayEmailId);
companyDetailList.add(comD);
companyContactDetail.add(companyname, companyDetailList);//cannot resolve method 'add(?,java util list..
}
return companyContactDetail; //incompatible type
}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
companyContactDetail = setCompanydata(); //unknown class company contact deatil
private void showFirstDialogwithList() {
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
builder.setCancelable(true);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("Contact");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.company_name);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(arrayAdapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
showSecondDialogwithList();
}
if (itemPosition == 1) {
alert.dismiss();
showSecondDialogwithList();
}
if (itemPosition == 2) {
alert.dismiss();
showSecondDialogwithList();
}
}
});
}
private void showSecondDialogwithList() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if(isPermissionGranted()){
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
My string arrays are
<string-array name="company_name">
<item>company 1</item>
<item>Company 2</item>
<item>Company 3</item>
</string-array>
<!-- AlertDialog way of Contact array -->
<string-array name="contact_way">
<item>Phone Call</item>
<item>Email</item>
</string-array>
<String-array name="phone">
<item>123456</item>
<item>125658</item>
<item>123451</item>
</String-array>
<String-array name="email">
<item>email1</item>
<item>email2</item>
<item>email2</item>
</String-array>
you can pass the position for the item and test it at your function or you can pass it directly :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alert.dismiss();
showSecondDialogwithList(position);
}
private void showSecondDialogwithList(int position) {
String phoneNumber;
switch (position) {
case 1:
phoneNumber = "123";
break;
case 2:
phoneNumber = "456";
break;
case 3:
phoneNumber = "789"
}
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if (isPermissionGranted()) {
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
To go deep in to logic behind that kind of scenario best way to use *HashMap*.
according to your scenario. i change in your code check it out:
public class CompanyDetail{
String companyPhoneNo;
String companyEmail;
public void setcompanyPhoneNo(String phoneNo){
this.companyPhoneNo = phoneNo;
}
public void setcompanyEmail(String Email){
this.companyEmail = Email;
}
public String getcompanyPhoneNo(){
return companyPhoneNo;
}
public String getcompanyEmail(){
return companyEmail;
}
}
public HashMap<String, List<CompanyDetail>> setCompanydata(){
int n = 3;
private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>();
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
for(int i=0;i<n;i++){
CompanyDetail comD= new CompanyDetail();
comD.setcompanyPhoneNo(companyPhoneno);
comD.setcompanyEmail(compnayEmailId);
companyDetailList.add(comD);
companyContactDetail.add(companyname, companyDetailList);
}
return companyContactDetail;
}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail;
companyContactDetail = setCompanydata();
private void showFirstDialogwithList() {
//Create a new builder and get the layout.
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
builder.setCancelable(true);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("Contact");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.company_name);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(arrayAdapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
}
if (itemPosition == 1) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
if (itemPosition == 2) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
}
});
}
private void showSecondDialogwithList( String companyName, List<CompanyDetail> companyDetail) {
CompanyDetail obj = companyDetail.get(0);
private String companyPhone = obj.getcompanyPhoneNo();
private String companyEmail = obj.getcompanyEmail();
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
builder.setView(dialogView);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
//Show the dislog
final AlertDialog alert = builder.show();
//Get the TextView, ListView, Button from the layout.
TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
final ListView alertListView = (ListView) dialogView.findViewById(listView1);
alertTitle.setText("What do you want to do");
// Defined Array values to show in ListView
String[] values = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, values);
alertListView.setAdapter(adapter);
alertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alert.dismiss();
}
});
alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if (itemPosition == 0) {
alert.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(companyPhone);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
if(isPermissionGranted()){
call_action();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if (itemPosition == 1) {
alert.dismiss();
......;
}
if (itemPosition == 2) {
dismiss();
showEmail();
}
if (itemPosition == 3) {
dismiss();
}
}
});
}
It's not a good practice to display two dialogs one by one. Of course you can implement it, like described above, but it will be much better to display separated activity(fragment) with the list(ListView, RecyclerView) of companies and click on each item will show the dialog with needed params. It will look better from design side and users experience.
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();
}
});
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();
}
Here is my code, that I am trying to do since last two days. All I can see is the listView in mainActivity and when the listitem is clicked a alertdialog box appears without any datas from database in textView. I think Cursor cursor is not working, that is why I cannot reach to my goal. Could anyone suggest me some good solution for this problem? Thanks in advance.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating a main list view
final ArrayList<String> getTrainingsList = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getTrainingsList);
ListView mylist = (ListView) findViewById(R.id.traininglistView);
mylist.setAdapter(adapter);
String Path = DB_PATH + DB_NAME;
//opening an existing database
final SQLiteDatabase db = SQLiteDatabase.openDatabase(Path, null, 0);
final String selectQuery = "SELECT * FROM training";
//Getting a cursor to fetch data from the database
final Cursor c = db.rawQuery(selectQuery, null);
Log.d("TAG","cursor found");
c.moveToFirst();
Log.d("TAG","Cursor moved to first");
if(c !=null){
//If there are contents in the database,
//then c!=null, so using do-while loop access data in database
do{
String title = c.getString(c.getColumnIndex("title"));
//String description = c.getString(c.getColumnIndex("description"));
//String title_date = title+":"+date;
getTrainingsList.add(title);
c.moveToNext();
}while(!c.isAfterLast());
//update the list
adapter.notifyDataSetChanged();
//closing the database after use
//db.close();
}
//Below is the code to alert alert dialogue window on item clicked
final TextView view1 = (TextView)findViewById(R.id.dialogTextView);
Cursor cursor = db.query(TABLE_TRAINING, new String[] { TRAINING_ID,
TRAINING_TITLE, TRAINING_DESCRIPTION, TRAINING_DATE, TRAINING_LOCATION },
TRAINING_ID + "=?",
new String[] { String.valueOf(TRAINING_ID) }, null, null, null, null);
Log.d("TAG","cursor found again");
if(cursor.getCount() > 0) {
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String location = cursor.getString(cursor.getColumnIndex("location"));
String allinfos = title+"\n"+description+"\n"+date+"\n"+location;
view1.setText(allinfos);
Log.d("TAG","Successfully set");
}
mylist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long _id) {
AlertDialog.Builder b= new AlertDialog.Builder(MainActivity.this);
b.setTitle("Title");
b.setView(view1);
b.setPositiveButton("Proceed to training", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(MainActivity.this, TraineeActivity.class));
}
});
b.setNegativeButton("Back to List", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
}
});
}`
Try to create a new textview instead of finding it in your activity layout.
That textView is already added to your layout maybe because of that you are not able to add it to your dialog
Try this:
//Below is the code to alert alert dialogue window on item clicked
final TextView view1 = new TextView(this);
Cursor cursor = db.query(TABLE_TRAINING, new String[] { TRAINING_ID,
TRAINING_TITLE, TRAINING_DESCRIPTION, TRAINING_DATE, TRAINING_LOCATION },
TRAINING_ID + "=?",
new String[] { String.valueOf(TRAINING_ID) }, null, null, null, null);
Log.d("TAG","cursor found again");
if(cursor.getCount() > 0) {
cursor.moveToFirst();
String title = cursor.getString(cursor.getColumnIndex("title"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String location = cursor.getString(cursor.getColumnIndex("location"));
String allinfos = title+"\n"+description+"\n"+date+"\n"+location;
view1.setText(allinfos);
Log.d("TAG","Successfully set");
}
mylist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long _id) {
AlertDialog.Builder b= new AlertDialog.Builder(MainActivity.this);
b.setTitle("Title");
b.setView(view1);
b.setPositiveButton("Proceed to training", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(MainActivity.this, TraineeActivity.class));
}
});
b.setNegativeButton("Back to List", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = b.create();
alertDialog.show();
}
});
You have to set the text after the dialog has been created and after calling alertDialog.show().
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();