Alert Dialog- Getting Error - android

I want to show the alert dialog inside a class which extends with arrayAdapter.
I am getting this error
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
Here my code, what i have tried
public class DeteteAdapter extends ArrayAdapter<Gold> {
private int contexts = Intent.FLAG_ACTIVITY_NEW_TASK;
private Context context;
private List<Gold> subjects = new ArrayList<Gold>();
private TextView subject;
private TextView date_day;
private TextView time;
private ProgressDialog pDialog;
String name;
JSONParser jsonParser = new JSONParser();
// url to create new product
private static String url_create_product = "http://ayyappagold.com/ayyappa/test.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
public static String month;
public static String year;
public DeteteAdapter(Context context, int textViewResourceId,
List<Gold> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.subjects = objects;
}
#Override
public int getCount() {
return this.subjects.size();
}
#Override
public Gold getItem(int index) {
return this.subjects.get(index);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
// ROW INFLATION
Log.d("ExamAdapter", "Starting XML Row Inflation ... ");
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_gold, parent, false);
Log.d("ExamAdapter", "Successfully completed XML Row Inflation!");
}
// Get item
Gold text = getItem(position);
subject = (TextView) row.findViewById(R.id.textView1);
time = (TextView) row.findViewById(R.id.textView2);
String content = text.content;
String times = text.time;
subject.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
name = getItem(position).id.toString();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Delete");
// set dialog message
alertDialogBuilder
.setMessage("This item will be deleted")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// current activity
new DeleteProduct().execute();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
subject.setText(content.replace("*", "\n"));
return row;
}
Plz help me to show the alert dialog in this class

I found the answer. And i am really happy to share with you,
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
v.getRootView().getContext());
Use v.getRootView().getContext() instead context or v.getContext()

Try this
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( DeteteAdapter.this);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
change this line like ;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
(Activity)context);
maybe it can help you

Related

AlertDialog in Adapter() Error Android Studio

AlertDialog.Builder dlg = new AlertDialog.Builder(context:??);
I get an error when I put "this"
No "activity.this"
#Override
public View getView(final int i, View view, final ViewGroup viewGroup) {
final View v = View.inflate(context, R.layout.trend_2019, null);
final TextView colorName = (TextView)v.findViewById(R.id.colorName);
TextView colorNameEn = (TextView)v.findViewById(R.id.colorNameEn);
TextView colorCode = (TextView)v.findViewById(R.id.colorCode);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("log", colorDataList.get(i).getColorCode());
AlertDialog.Builder dlg = new AlertDialog.Builder(view.getContext());
dlg.setNegativeButton("닫기", null);
dlg.show();
}
});
v.setTag(colorDataList.get(i).getColorCode());
return v;
}
You should pass context in constructor of adapter
public class ColorAdapter extends BaseAdapter {
private Context context;
private List<ColorData> colorDataList;
public ColorAdapter(Context context, List<ColorData> colorDataList) {
this.context = context;
this.colorDataList = colorDataList;
}
and use that context in your dialog and you are missing dlg.create() in your code
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setNegativeButton("닫기", null);
dlg.create();
dlg.show();
It will solve that error
Before show AlertDialog you should create it.
Call create() before show()
Your edited code
AlertDialog.Builder dlg = new AlertDialog.Builder(view.getContext());
dlg.setNegativeButton("닫기", null);
dlg.create();
dlg.show();

How to get custom dialog activity while clicking on a button in listview in android?

here is my code for ArrayAdapter. When i click on LinearLayout "cat" it gives error on dialog.show(). I don't know how to create custom dialog within ArrayAdapter class.
Everything work fine when i remove creating dialog part.
Thanks in advance
CategoryAdapter.java
public class CategoryAdapter extends ArrayAdapter<String> {
private final Context context;
String[] menu = new String[25] ;
String[] menu2 = new String[25];
String[] menu3 = new String[25];
private LayoutInflater inflater;
viewholder vh;
public CategoryAdapter(Context context, String [] menu,String [] menu2,String [] menu3) {
super(context, R.layout.categoryadapter, menu);
this.context = context;
this.menu = menu;
this.menu2=menu2;
this.menu3=menu3;
}
public View getView(final int position, View convertView, ViewGroup parent) {
{
vh=new viewholder();
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.categoryadapter, parent, false);
vh.cat=(LinearLayout) convertView.findViewById(R.id.category);
convertView.setTag(vh);
}
vh.cat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
public class viewholder
{
LinearLayout cat;
}
}
Use context instead getContext()
final Dialog dialog = new Dialog(context);
Finally, Just pass context
vh.cat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
Toast.makeText(getContext(), "Clicked", Toast.LENGTH_LONG).show();
}
});
Try-
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.update_categore_dialog);
dialog.setTitle("Update Your Category");
dialog.show();
try this below link, hope you like it.
OrderDetailListAdatper adapter = new OrderDetailListAdatper(Yourclass.this,Resource,
listorderlistInfo);
//set your adapter..
in your adapter getview, paste that code and it works fine for me.
holder.btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder((Activity)_context);
alert.setMessage("Do you want to delete?");
alert.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
});
alert.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(
DialogInterface dialog,
int whichButton) {
OrderDetailListAdatper.this._listOrderListInfoAdapter
.remove(position); OrderDetailListAdatper.thisnotifyDataSetChanged();
}
});
alert.create().show(); // btw show() creates and shows it..
}
});
In your getView() method, Replace getContext() with your context variable.
vh.cat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
//remaining code...
}
});
And in your initialization of CateogoryAdapter don't pass getApplicationContext(), instead pass in the context (Activity.this) like this.
CategoryAdapter adapter = CategoryAdapter(YourActivity.this, menu, menu2, menu3)

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;
}
}

loading a default image using bitmap factory inside list view Adapter Class

i am calling a web service to get some details and show them in a list view.now i have to show a image on that list view. i can retrieve the image url from JSON object. but when the image url contains null , i want to show a defult image in the list view. i know below code is the code segment which is use to that.but since im going to handle this inside of my adapter class (extends by BaseAdapter Class) i cant use it.. please guide me how to handle this...
here my Adapter class
public class NewsRowAdapter extends BaseAdapter {
static Dialog dialogs;
private static final String STIME = "StartTime";
private static final String END = "EndTime";
private static final String DATE = "Date";
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
public ImageLoader imageLoader;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
this.mContext = ctx;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public void showFirstDialog(final ArrayList<HashMap<String, String>> list){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("You Have Similar Kind of Appoinments!! Do you wanna Show them ?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(mContext, "Showing", Toast.LENGTH_LONG).show();
dialogpop(list);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
public void dialogshow(final String Date,final String Start,final String End){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes Confirm!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
//MainActivity.this.finish();
Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
//check similer records
//if duplicates > 1 then show the popup list
//if(duplicateList.size()>1){
/*final Dialog dialogs = new Dialog(activity);
dialogs.setContentView(R.layout.dialog_list);
dialogs.setTitle("Select One");
ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);
NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, duplicateList);
listView.setAdapter(nw);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
dialogs.dismiss();
}
});
dialogs.show();*/
// }
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){
//CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
LayoutInflater infl = activity.getLayoutInflater();
View view = infl.inflate(R.layout.dialog_list, null);
ListView lv = (ListView) view.findViewById(R.id.dialogList);
//NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);
SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row, new String[] { STIME,END, DATE }, new int[] {
R.id.stime2,R.id.etime2, R.id.blank2});
lv.setAdapter(sim);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
}
});
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.two_line_list_item, android.R.id.text1, Names);*/
alertDialogBuilder2.setView(view)
/*alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
}
})
*/
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialogBuilder2.show();
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
final TextView firstname = (TextView) vi.findViewById(R.id.fname);
final TextView lastname = (TextView) vi.findViewById(R.id.lname);
final TextView startTime = (TextView) vi.findViewById(R.id.stime);
final TextView endTime = (TextView) vi.findViewById(R.id.etime);
final TextView date = (TextView) vi.findViewById(R.id.blank);
final TextView hidID = (TextView) vi.findViewById(R.id.hidenID);
final ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
endTime.setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_DATE));
hidID.setText(song.get(MainActivity.TAG_HID));
String theUrl = song.get(MainActivity.TAG_IMG);
if(theUrl.equalsIgnoreCase("null")){
/*Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.propic);
profPic.setImageBitmap(bImage);
ViewList v = new ViewList();
v.handleImage(theUrl, img);*/
}
else{
imageLoader.DisplayImage(song.get(MainActivity.TAG_IMG), img);
}
Button accept = (Button) vi.findViewById(R.id.btnAccepted);
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int x = (int) getItemId(position);
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
// get the intent from the hashmap check if there is similar date and time.
//then store them in a list or array.
String getDate = (String) date.getText();
String getStartTime = startTime.getText().toString();
String getEndTime = endTime.getText().toString();
ShortList sh = new ShortList();
ArrayList<HashMap<String, String>> duplicateList;
duplicateList=sh.getDuplicated(getDate, getStartTime, getEndTime);
if(duplicateList.size()>1){
//dialogshow(getDate,getStartTime,getEndTime);
showFirstDialog(duplicateList);
}
else{
dialogshow(getDate, getStartTime, getEndTime);
}
}
});
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getPname = hidID.getText().toString();
Toast.makeText(parent.getContext(), "view clicked: "+getPname , Toast.LENGTH_SHORT).show();
//get the id of the view
//check the id of the request
//call the web service acording to the id
Intent zoom=new Intent(parent.getContext(), Profile.class);
zoom.putExtra("PatientID", getPname);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
public void dialogpop(ArrayList<HashMap<String, String>> list){
dialogs = new Dialog(activity);
dialogs.setContentView(R.layout.dialog_list);
dialogs.setTitle("Select One");
ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);
//SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row, new String[] { STIME,END, DATE }, new int[] {
// R.id.stime2,R.id.etime2, R.id.blank2});
Adapter_For_Dialog nw = new Adapter_For_Dialog(mContext,activity, R.layout.dialog_row, list);
listView.setAdapter(nw);
dialogs.show();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
#Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
please help me :)
My problem is i cant use this code segment in my adapter Class
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.propic);
profPic.setImageBitmap(bImage);
Replace
if(theUrl.equalsIgnoreCase("null")){
with
if(theUrl==null || theUrl.equals("")){
and check if it works or not

how to set a custom list view into a dialog box

I am developing a application which fetches some data from a web service and displays in a list view. I have implemented a custom adapter which is extended by BaseAdapter. In the getView() method I inflate the raw also.. Those are working perfectly.
My problem is I have implemented code to show a dialog box when user click on an list item, but now I want to show another dialog box which has a custom list inside it (when Yes button clicked). I also want to show some data in that listview. [I have a ArrayList filled with the data that I wanted] . I'm writing the code inside my adapter class. Can anyone give me some idea how to do it ?
This is my code:
public class NewsRowAdapter extends BaseAdapter {
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
this.mContext = ctx;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void dialogshow(final String Date,final String Start,final String End){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
//MainActivity.this.finish();
// Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
//check similer records
// ShortList sh = new ShortList();
// ArrayList<HashMap<String, String>> duplicateList;
// duplicateList=sh.getDuplicated(Date, Start, End);
//if duplicates > 1 then show the popup list
// if(duplicateList.size()>1){
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
LayoutInflater infl = activity.getLayoutInflater();
//View vi = infl.inflate(id, root)
alertDialogBuilder2.setView(infl.inflate(R.layout.dialog_row, null))
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialogBuilder2.show();
// }
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
final TextView firstname = (TextView) vi.findViewById(R.id.fname);
final TextView lastname = (TextView) vi.findViewById(R.id.lname);
final TextView startTime = (TextView) vi.findViewById(R.id.stime);
final TextView endTime = (TextView) vi.findViewById(R.id.etime);
final TextView date = (TextView) vi.findViewById(R.id.blank);
final ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
endTime.setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_DATE));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
Button accept = (Button) vi.findViewById(R.id.button1);
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int x = (int) getItemId(position);
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
// get the intent from the hashmap check if there is similar date and time.
//then store them in a list or array.
String getDate = (String) date.getText();
String getStartTime = startTime.getText().toString();
String getEndTime = endTime.getText().toString();
dialogshow(getDate,getStartTime,getEndTime);
}
});
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getFname = firstname.getText().toString();
Toast.makeText(parent.getContext(), "view clicked: "+getFname , Toast.LENGTH_SHORT).show();
//get the id of the view
//check the id of the request
//call the web service acording to the id
Intent zoom=new Intent(parent.getContext(), Profile.class);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
You are on the right track, Here is what i used to dynamically display a Dialog with a list of items in it.
For reference a similar Question was asked here : Android custom list dialog
//String[] list_data; Preloaded with a String array
final CharSequence[] items = new CharSequence[list_data.length];
for (int i = 0; i < list_data.length; i++) {
items[i] = list_data[i];
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select the data you want");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Get the id of the item
diag_callback();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void diag_callback() {
//Do someting when the user has made his selection
}
Hope this sorts out your problem.
For your concern I hope that you know creating customized list view.
Fallow undermentioned code that contains customized dialog with customized list view. I hope you can perform Adapter and model part by your's
final Dialog new_dialog = new Dialog(getParent());
new_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
new_dialog.setContentView(R.layout.customize_dialog_list_view);
new_dialog.setCancelable(false);
cuc = new CommanUtilityClass();
SharedPreferences sp = getSharedPreferences("provider",0);
String services = sp.getString("services","");
Log.i("Servicesss",bAllServices);
TextView service = (TextView) new_dialog.findViewById(R.id.cdlv_service_provider);
TextView hour = (TextView) new_dialog.findViewById(R.id.cdlv_working_hours);
TextView appointment_time = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_time);
TextView appointment_date = (TextView) new_dialog.findViewById(R.id.cdlv_appoint_date);
String[] ampm = myTiming[which].split(":");
Log.d("xxxooo", ampm[0]);
appointment_time.setText(Html.fromHtml("<b>Appointment time :</b>" + myTimingToShow[which].split("/")[0]));
appointment_date.setText(Html.fromHtml("<b>Appointment date :</b>" + selected));
service.setText(Html.fromHtml("<b>Service provider :</b>"+ cuc.toTheUpperCase(bsp_name)));
hour.setText(Html.fromHtml("<b>Working hours :</b>"+ cuc.toTheUpperCase(bsp_availability)));
lv = (ListView) new_dialog.findViewById(R.id.cdlv_list);
CustomDialogArrayAdapter cdaa = new CustomDialogArrayAdapter(getApplicationContext(),m_ArrayList);
lv.setAdapter(cdaa);
new_dialog.show();
ImageButton btn_cdlv_cancel = (ImageButton) new_dialog.findViewById(R.id.cdlv_cancel);
btn_cdlv_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new_dialog.dismiss();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(
AdapterView<?> arg0,
View arg1,
int arg2,
long arg3) {
Log.d("checkforbookedslots", booked_slots);
final_time_selected = myTiming[which];
String final_duration = m_ArrayList.get(arg2).provider_service_duration;
Log.d("adiadicheck", check_for_booking_list);
if (checkOverlapSlot(final_time_selected,final_duration)) {
Log.d("gp","seven12aa");
SharedPreferences sp = getSharedPreferences("booking_detail",0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("provider_service",m_ArrayList.get(arg2).provider_service);
editor.putString("provider_service_duration",m_ArrayList.get(arg2).provider_service_duration);
editor.putString("provider_service_price",m_ArrayList.get(arg2).provider_service_price);
editor.putString("service_id",m_ArrayList.get(arg2).provider_service_id);
editor.commit();
SessionManagement sm = new SessionManagement(getParent());
// sm.checkLogin();
if (sm.isLoggedIn()) {
Log.d("viv2","1");
Intent edit = new Intent(SelectedService.this,UserLoggedActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("UserLoggedActivity",edit);
} else {
Log.d("viv2","2");
Intent edit = new Intent(SelectedService.this,UserNoLoggedActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("UserNoLoggedActivity",edit);
}
new_dialog.dismiss();
}
}
});
CUSTOM DIALOG BOX WITH CUSTOM LISTVIEW :::
public class MainActivity extends Activity {
private Button btnOpenDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> strArrlst = new ArrayList<String>();
for (int i = 0; i < 15; i++) {
strArrlst.add("Number: " + i);
}
btnOpenDialog = (Button) findViewById(R.id.btn_open_dialog);
btnOpenDialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Dialog box is btn click", "");
final Dialog dialog = new Dialog(MainActivity.this);
Toast.makeText(getApplicationContext(), "btn is click", Toast.LENGTH_SHORT).show();
// tell the Dialog to use the dialog.xml as it's layout
// description
dialog.setContentView(R.layout.custom_dailog_box);
dialog.setTitle("Android Custom Dialog Box");
TextView txt = (TextView) dialog.findViewById(R.id.txt);
txt.setText("This is an Android custom Dialog Box Example! Enjoy!");
ListView dialogLIst = (ListView) dialog.findViewById(R.id.lstvw_open_custom_dialog);
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, android.R.id.text1,
// values);
// dialogLIst.setAdapter(adapter);
AdapterListC adapListC = new AdapterListC(getApplicationContext(), strArrlst);
dialogLIst.setAdapter(adapListC);
dialogLIst.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
dialog.dismiss();
Toast.makeText(getApplicationContext(), "This is click position is" + position, Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
});
}
#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;
}
}
ADAPTER CLASS:::
public class AdapterListC extends BaseAdapter {
public AdapterListC(Context context_, ArrayList<String> arrlstString) {
super();
this.context_ = context_;
this.arrlstString = arrlstString;
mInflater = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
Context context_;
ArrayList<String> arrlstString;
private LayoutInflater mInflater;
#Override
public int getCount() {
// TODO Auto-generated method stub
return arrlstString.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrlstString.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Viewolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, null);
holder = new Viewolder();
holder.txtName = (TextView) convertView.findViewById(R.id.txt_listitem);
convertView.setTag(holder);
} else {
holder = (Viewolder) convertView.getTag();
}
holder.txtName.setText(arrlstString.get(position).toString());
return convertView;
}
class Viewolder {
TextView txtName;
}
}

Categories

Resources