on click item is not working inside dialog..
Description: i have one print button when i click that button a dialog will appear which contains listview using adapter.. when i click on listview row it must go to other activity.. But problem is onitemclick is not working in listview dialog..
thank u in advance.
WorkDetailView.java
printbutton = (Button) findViewById(R.id.barcodePrintButton);
printbutton.setOnClickListener(new OnClickListener() {
private Context context = WorkDetailView.this;
/**
*
* Description: barcodePrintButton onClick to ServiceManager.callPrintService
*
*/
#Override
public void onClick(View view) {
dialog = new Dialog(context);
dialog.setContentView(R.layout.dialoglistview);
dialog.setTitle("Print Dialog");
listviewDialog = (ListView) dialog.findViewById(R.id.dialogList);
dataDialogAdapter = new WorkDetailDialogAdapter(context, sampleDetailsArray);
listviewDialog.setAdapter(dataDialogAdapter);
listviewDialog.setClickable(true);
listviewDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Log.i("selectedAdapter", "selectedAdapter"+ position);
Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
intent.putExtra("ENCODE_DATA", patient.getMrnNumber());
intent.putExtra("ENCODE_FORMAT", "CODE_128");
// intent.putExtra("ENCODE_SHOW_CONTENTS", false);
startActivity(intent);
Toast.makeText(WorkDetailView.this, "Printing Barcode Result", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
});
Did you try to change the type of onClickListener to Dialog.onClickListener?
android.content.DialogInterface.OnClickListener ocl = new Dialog.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
};
try this way to build your dialog
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialoglistview,
(ViewGroup) findViewById(R.id.root_id_in_dialoglistview_xml_tree));
dialog = AlertDialog.Builder(this).setTitle("Print Dialog").setView(layout);
Place dialog.show() before listviewDialog.setClickable(true)
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showdialog();
}});
private void showdialog() {
listDialog = new Dialog(this);
listDialog.setTitle("Select Item");
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.list_view, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
final ListView list1 = (ListView) listDialog.findViewById(R.id.listView1);
String array[] = {"1","2","3"};
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.group, array);
list1.setAdapter(adapt);
listDialog.show();
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
System.out.println(position);
}
});
}
Related
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)
I have been working and messing with my code from last two days.
I have a List View in a Fragment and a Button .
On clicking a button i add the data to List View entered through Dialog
What I am unable to do is refresh the ListView after clicking Ok Button.
Here is my Code
However it loads when i exit the app and resume again
Any Help will be appreciated
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
final EditText input = new EditText(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
return new AlertDialog.Builder(getActivity())
.setView(input)
.setIcon(R.drawable.ic_launcher)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String Value = input.getText().toString();
String normalizednumber = NormalizePhoneUtilities
.normalizePhoneNumber(Value);
ContactModel contactModel = new ContactModel(
"Hitesh", normalizednumber, String
.valueOf(3));
ContactsDatabaseWorker sm = new ContactsDatabaseWorker(
getActivity());
sm.AddSmartContact(contactModel);
databaseWorker = new ContactsDatabaseWorker(
getActivity());
// LinkedList<Cursor> list = new
// LinkedList<Cursor>();
// list=databaseWorker.getAllData();
run = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
customAdapter = new CustomCursorAdapter(
getActivity(), databaseWorker
.getAllData());
customAdapter.notifyDataSetChanged();
listview.invalidateViews();
listview.refreshDrawableState();
listview.setAdapter(customAdapter);
}
};
getActivity().runOnUiThread(run);
isModal = false;
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (isModal) // AVOID REQUEST FEATURE CRASH
{
View view = inflater.inflate(R.layout.smartalert_fragment,
container, false);
listview = (ListView) view.findViewById(R.id.lvSmartAlert);
return super.onCreateView(inflater, container, savedInstanceState);
}
View view = inflater.inflate(R.layout.smartalert_fragment, container,
false);
Button bAdd = (Button) view.findViewById(R.id.bAddContact);
bAdd.setOnClickListener(this);
listview = (ListView) view.findViewById(R.id.lvSmartAlert);
new Handler().post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
customAdapter = new CustomCursorAdapter(getActivity(),
databaseWorker.getAllData());
listview.setAdapter(customAdapter);
}
});
return view;
}
Call notifyDataSetChanged (); in adapter.
Your CursorAdapter has a swapCursor() method, which you can use each time you get an updated cursor.
This is my MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
ArrayList<Product> products = new ArrayList<Product>();
final Context context = this;
ListAdapter boxAdapter;
String[] dataArray;
EditText editText;
//name that get back through the dialog
private String getName;
private String selectedItem;
private ArrayAdapter<String> adapter; // The list adapter
/** Called when the activity is first created. */
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
fillData();
boxAdapter = new ListAdapter(this, products);
ListView lvMain = (ListView) findViewById(R.id.lvMain);
lvMain.setAdapter(boxAdapter);
Button btn = (Button) findViewById(R.id.AddItem);
//the add item button function
btn.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
//get the dialog view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.insert);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// get user input and set it to result
// edit text
getName=userInput.getText().toString();
products.add(new Product(getName,false));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
// Create the listener for normal item clicks
OnItemClickListener itemListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long rowid) {
Toast.makeText(
getApplicationContext(),
"You have clicked on " + parent.getItemAtPosition(position).toString() + ".",
Toast.LENGTH_SHORT).show();
}
};
//the long press to delete function
OnItemLongClickListener itemLongListener = new OnItemLongClickListener()
{
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
// Store selected item in global variable
selectedItem = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to remove " + selectedItem + "?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
products.remove(selectedItem);
boxAdapter.notifyDataSetChanged();
Toast.makeText(
getApplicationContext(),
selectedItem + " has been removed.",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Create and show the dialog
builder.show();
// Signal OK to avoid further processing of the long click
return true;
}
};
lvMain.setOnItemClickListener(itemListener);
lvMain.setOnItemLongClickListener(itemLongListener);
}
void fillData() {
dataArray = getResources().getStringArray(R.array.ChecklistData);
for(String productName : dataArray)
{
products.add(new Product(productName,false));
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
This is ListAdapter.java
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
/*((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);*/
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.box);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
}
OnItemClickListener and OnItemLongClickListener didn't function at all. Anyone help me out here.. I did diagnose the problem but it still doesn't have function
OnItemClickListener itemListener = new OnItemClickListener() {
OnItemLongClickListener itemLongListener = new OnItemLongClickListener() {
You just define those variables but you don't assign them to your ListView.
You should call these lines somewhere:
lvMain.setOnItemClickListener(itemListener);
lvMain.setOnItemLongClickListener(itemLongListener);
UPDATE:
You also miss to register the list for context menu.
registerForContextMenu(lvMain);
implement View.OnItemClickListener and AdapterView.OnItemLongClickListener and their corresponding methods in your class.
initialize your views correctly (findViewById...)
set click listeners to your views (button.setOnClickListener(this) / button.setOnLongClickListener(this)
react to the push events in the implemented methods like:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
this.doSomething();
}
}
and
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//react to the view or react to the position
switch (view.getId()) {
case R.id.button:
this.doSomething();
}
return true;
}
You need to register listeners. Add following lines to the end of your onCreate() method.
lvMain.setOnItemClickListener(itemListener);
lvMain.setOnItemLongClickListener(itemLongListener);
I had the exact same problem. When onclick or onlongclick are enabled in the Layout; onitemclickListener wont work. Just remove the tags android:clickable and android:longclickable from your Layout XML resource and it will all work.
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;
}
}
whenever user do tap on list item row i am trying to show dialog, but getting nothing whenever i do click on list item row, why?
Here I just want to update value of total by accepting new qty from user in dialog via show that value in place of quantity in a List row ....
CartActivity.java:
public class CartActivity extends Activity {
ListView mLstView1;
CartAdapter mViewCartAdpt;
final private static int DIALOG_QUANTITY = 1;
EditText update_quantity ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mLstView1 = (ListView) findViewById(R.id.listView1);
mViewCartAdpt = new CartAdapter(CartActivity.this);
mLstView1.setAdapter(mViewCartAdpt);
mLstView1.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("deprecation")
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id)
{
showDialog(DIALOG_QUANTITY);
}
});
}
protected Dialog onCreateDialog(int id) {
AlertDialog dialogDetails = null;
switch (id) {
case DIALOG_QUANTITY:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setTitle("Image Information");
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
}
return dialogDetails;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_QUANTITY:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button updateQuantityButton = (Button) alertDialog
.findViewById(R.id.btn_login);
Button cancelbutton = (Button) alertDialog
.findViewById(R.id.btn_cancel);
update_quantity = (EditText) alertDialog
.findViewById(R.id.edit_new_qty);
updateQuantityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
break;
}
}
// Called when the activity begins interacting with the user
#Override
protected void onResume(){
super.onResume();
mViewCartAdpt.notifyDataSetChanged();
}
}
CartAdapter.java:
public class CartAdapter extends BaseAdapter {
public static final String LOG_TAG = "CartAdapter";
public static final String KEY_TITLE = "title";
public static final String KEY_COST = "cost";
public static final String KEY_QTY = "qty";
public static final String KEY_TOTAL = "total";
Activity activity;
LayoutInflater inflater;
ImageButton mImgBtnDelete;
ListView listView;
private double itemamount = 0;
private int itemquantity = 0;
public CartAdapter(Activity a) {
// TODO Auto-generated constructor stub
activity = a;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return Constants.sItem_Detail.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listrow_cart, null); // listrow_cart
vi.setClickable(true);
vi.setFocusable(true);
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
}
});
final TextView title = (TextView) vi.findViewById(R.id.title);
final TextView qty = (TextView) vi.findViewById(R.id.qty);
final TextView cost = (TextView) vi.findViewById(R.id.cost);
final TextView total = (TextView) vi.findViewById(R.id.total);
HashMap<String, String> item = new HashMap<String, String>();
item = Constants.sItem_Detail.get(position);
// Setting all values in listview
title.setText(item.get(com.example.sample.ItemsActivity.KEY_TITLE));
cost.setText(item.get(com.example.sample.ItemsActivity.KEY_COST));
qty.setText("1");
itemquantity = Integer.parseInt(qty.getText().toString());
itemamount = Double.parseDouble(cost.getText().toString());
total.setText(new DecimalFormat("##.#").format(itemamount*itemquantity));
return vi;
}
}
Take your Dialog inside your activity, where you have to do code for listiview click, inside click show your dialog,
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
//show dialog.
}
});
I can see, your dialog having two buttons and one editbox, and whatever values is to be input in editbox, you updating your list value with editbox value.
Grab editbox value and update your list array, which you are setting on adapter to fill your listview.
updateQuantityButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you have to fetch editbox value and update your arraylist and then notify your adapter.
}
});
I believe this is because onCreateDialog() and onPrepareDialog() should be overriden in an activity, not in the adapter.
Since you declared activity to be of type Activity, calling activity.showDialog() won't have any effect.
What you have to do, is to move those methods in the activity you want the dialogs to be displayed, then pass to the CardAdapter and instance of your activity and use it in the adapter.
new CardAdapter(MyActivity.this);
//...
public class CardAdapter .... {
public MyActivity activity;
//.......
activity.showDialog
}
Though, I would suggest to use DialogFragment instead.
you can use this way when click on item of the list view then open the dialog box and whatever changes or update in list then use setNotifyDataChanged() before set the adapter on list view
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
yourActivty.this.showDialog(DIALOG_QUANTITY);
}
});