I have a custom dialog created inside an Adapter, which has two buttons ((+)ve and (-)ve), (+)ve vl call another service and (-)ve will simply dismiss the dialog.
public class BookingAdapter extends ArrayAdapter<BookingItem> {
private Dialog aDialog;
private Context context;
private Activity activity;
public BookingAdapter (Context context, int resource, List<BookingItem> objects) {
super(context, resource, objects);
this.context = context;
this.activity = (Activity) context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// some other tasks being done
onButtonClick(position, holder);
return convertView;
}
public void onButtonClick(final int position, ViewHolder viewHolder){
// some other tasks being done
makeDialog(name, id, date, cost);
}
public void makeDialog(final String name, final String Id, final String date, String cost){
Button confirmButton;
Button cancelAndReturnButton;
TextView confirmDialogBody;
aDialog = new Dialog(activity, R.style.Dialog_No_Border);
aDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater m_inflater = LayoutInflater.from(activity);
View m_view = m_inflater.inflate(R.layout.casual_day_booking_confirm_dialog, null);
// set Buttons and Texts
View.OnClickListener m_clickListener = new View.OnClickListener(){
#Override
public void onClick(View p_v) {
switch (p_v.getId()) {
case R.id.confirm_and_proceed:
PlaceBookingTask bookingTask = new PlaceBookingTask(date,Id,context);
bookingTask.execute();
break;
case R.id.cancel_and_return:
aDialog.dismiss();
break;
default:
break;
}
}
};
aDialog.setContentView(m_view);
if(!activity.isFinishing()) {
aDialog.show();
}
}
#Subscribe
public void onSuccess(PlaceBookingEvent event){
PlaceBookingResponse response = event.getPlaceBookingResponse();
aDialog.dismiss();
secondConfirmationDialog(response.getName, response.getDate, response.getId);
}
public void secondConfirmationDialog(String name,String date,String id) {
// this will create another dialog
}
}
When onSuccess, aDialog.dismiss(), aDialog is null and cannot be dismissed.
i checked some similar questions in SO like this and this, i'm doubt that only AlertDialog.Builder is the solution for my problem.
Try removing the "private" in your variable.
I think your dialog implementation is wrong. Try with the following.
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Related
I'm making a list view using a custom adapter.
In that, I want show a dialog when each item is clicked (surely, a different dialog at each list).
Here is my code.
MainActivity:
public class MainActivity extends AppCompatActivity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<MyCustomList> list = new ArrayList<MyCustomList>();
list.add(new MyCustomList("a", R.string.jangho_string1, R.string.jangho_string2, R.drawable.jangho));
list.add(new MyCustomList("b", R.string.dae_string1, R.string.dae_string2, R.drawable.dae));
list.add(new MyCustomList("c", R.string.an_string1, R.string.an_string2, R.drawable.an));
listView = (ListView)findViewById(R.id.listviewone);
MyCustomAdapter adapter = new MyCustomAdapter(getApplicationContext(), R.layout.listviewone, list);
listView.setAdapter(adapter);
}
}
MyCustomAdapter
public class MyCustomAdapter extends BaseAdapter {
Context ctx;
int layout;
ArrayList<MyCustomList> list;
LayoutInflater inf;
public MyCustomAdapter(Context ctx, int layout, ArrayList<MyCustomList> list){
this.ctx = ctx;
this.layout = layout;
this.list = list;
inf = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = inf.inflate(layout, null);
}
final View.OnClickListener makeListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
((Activity)ctx).showDialog(position);
}
};
convertView.setOnClickListener(makeListener);
return convertView;
}
protected Dialog onCreateDialog(int position){
Dialog dialog = null;
switch (position){
case 0:
break;
}
return dialog;
}
}
I'm using Android Studio.
you can create a method to show dialog, and pass the values from the listItem which is clicked
public void showDialog(Context context, String something){
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
//Initialize your widgets from layout here
dialog.show();
//You can dialog.dismiss() to close the dialog.
}
Firstly,use ViewHolder when using listview. And just use listview.setOnItemClickListener
try this to create Custom Dialog
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer here:- http://coderzpassion.com/android-show-alertdialog/
i have a listView like THIS
i want when i press the delete. it show a dialog like this image
so when i press YES. it will remove from list.
here's my code..
public class customadapter extends BaseAdapter{
ArrayList<HashMap<String, String>> oslist;
Context context;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return oslist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return oslist.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
System.out.println("oslist oslist = "+oslist);
System.out.println("oslist 1 = "+oslist);
System.out.println("oslist size = "+oslist.size());
System.out.println("oslist oslist = "+oslist.getClass());
System.out.println("position = "+position);
System.out.println("convertView = "+convertView);
System.out.println("parent = "+parent);
System.out.println("position = "+position);
LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = lif.inflate(R.layout.listitem, null);
TextView id = (TextView) convertView.findViewById(R.id.varId);
TextView noNota = (TextView) convertView.findViewById(R.id.varNoNota);
TextView senderName = (TextView) convertView.findViewById(R.id.varSenderName);
TextView totalAmount = (TextView) convertView.findViewById(R.id.varTotalAmount);
id.setText(oslist.get(position).get("id"));
noNota.setText(oslist.get(position).get("noNota"));
senderName.setText(oslist.get(position).get("senderName"));
totalAmount.setText(oslist.get(position).get("totalAmount"));
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Edit ditekan!", Toast.LENGTH_LONG).show();
//I want show YES NO dialog here.
}
});
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//I want show YES NO dialog here
}
});
return convertView;
}
}
how can i do to create a dialog like that..i tried this code
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
but itsnt success.
i got this error
Create an interface:
public interface OnDeleteListener {
public void onDelete(String message);
}
When you are initializing customadapter send OnDeleteListener as a parameter:
private OnDeleteListener mListener;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, OnDeleteListener mListener) {
this.context = context;
this.oslist = oslist;
this.mListener = mListener;
}
then on delete button click check for listener to whether activate it or not:
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//I want show YES NO dialog here
if(mListener != null)
mListener.onDelete("The message you want to show");
}
});
and finally initialize adapter in your activity/fragment and on listener invoke show Dialog:
customadaper mAdapter = new customadapter(ArrayList<HashMap<String, String>> oslist,Context context, new OnDeleteListener(){
#Override
public void onDelete(String msg){
//Show your dialog here
//msg - you can send any parameter or none of them through interface just as an example i send a message to show
showDialog(msg);
}
});
You can create a seperate function for code clearance and call it whenever you want to use
(also notice that, to create a custom dialog you have to inflate it {probably thats why you are getting an error}):
private void showDialog(String message){
// set the custom dialog components - text, image and button
inflater = mInflater.inflate(R.layout.your_custom_dialog, null, false);
TextView text = (TextView) inflater.findViewById(R.id.text);
text.setText(message);
ImageView image = (ImageView) inflater.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher); //line 115
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);
mDialogBuilder.setView(viewFilterDialog);
mDialogBuilder.setCancelable(true);
mDialogBuilder.setTitle(mRes.getString(R.string.dialog_title_filter));
...
final AlertDialog mAlertDialog = mDialogBuilder.create();
mAlertDialog.show();
note: I have hardcoded this answer so any syntax error can occur
try this code.For a Dialog you have to use Activity's instance not application context.
final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);
first pass the activity like this.
list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));
then get the parent activity like this..
private Activity parentActivity;
private Button btnDelete;
private Button btnEdit;
AlertDialog.Builder alertDialogBuilder;
public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {
System.out.println("skdjfhksdfjskfjhsdkjfh");
this.context = context;
this.oslist = oslist;
this.parentActivity = parentactivity;
}
and set dialog builder like this..
final Dialog dialog = new Dialog(parentActivity);
You can also do this in activity. For Adapter code, add some tags to your button to identify which button is pressed. You can also set position as a tag.
Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);
btnEdit.setTag(oslist.get(position).get("id"),R.id.varId);
btnDelete .setTag(oslist.get(position).get("id"),R.id.varId);
You can add this in xml for your custom dialog.
android:onclick="deleteMethod"
Write your method in activity like this :
public void deleteMethod(View v)
{
// Get your id or position for which delete button was pressed
String id=v.getTag().toString();
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to delete ?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this, "Deleting...", Toast.LENGTH_SHORT).show();
// You know which item to delete from id / position. delete here.
}})
.setNegativeButton(android.R.string.no, null).show();
}
I think there is a easy way to do this, in my case i have followed this code.
write a method alertMessage() and call this inside your button listener code
btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//I want show YES NO dialog here
alertMessage();
}
});
public void alertMessage() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE: // Yes button clicked
Toast.makeText(AlertDialogActivity.this, "Yes Clicked",
Toast.LENGTH_LONG).show();
// set your own logic for removal item from listview
break;
case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing
Toast.makeText(AlertDialogActivity.this, "No Clicked",
Toast.LENGTH_LONG).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
dialog will dismiss itself after user click "YES" or "NO" button. There is nothing you need to do
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);
}
});
I have a listview with an ArrayAdapter. The ListView at a position fills itselfs with the same position in some ArrayList. In each listitem
there are two buttons. When the onClickListener start, it gets
the position of the overrided View getView(position, View etc.), takes some variables out of the ArrayList and sends it to a database. The problem is, the position is not really accurate. Sometimes I get the position of a Listitem above the Listitem I want. Here is the code:
public class MemoAdapter extends ArrayAdapter<String> {
private final Activity context;
ArrayList<String> commentarray;
ArrayList<String> fromarray;
ArrayList<String> datearray;
ArrayList<String> ids;
ArrayList<String> feedback;
Dialog dialog;
EditText feedbacktxt;
String token;
String website;
public MemoAdapter(Activity context, ArrayList<String> commentArray,
ArrayList<String> fromArray, ArrayList<String> dateArray,
ArrayList<String> ids, ArrayList<String> feedback, String token,
String website) {
super(context, R.layout.memo_listitem, commentArray);
this.context = context;
this.commentarray = commentArray;
this.fromarray = fromArray;
this.datearray = dateArray;
this.ids = ids;
this.feedback = feedback;
this.token = token;
this.website = website;
}
#Override
public long getItemId(int position) {
return 0;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public TextView textView;
public TextView textView2;
public TextView textView3;
public TextView textView4;
// public ImageView reply;
// public ImageView accept;
public Button reply;
public Button accept;
public RelativeLayout r;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.memo_listitem, null);
holder = new ViewHolder();
holder.textView = (TextView) rowView
.findViewById(R.id.memodescription);
holder.textView2 = (TextView) rowView.findViewById(R.id.memofrom);
holder.textView3 = (TextView) rowView.findViewById(R.id.memodate);
holder.textView4 = (TextView) rowView
.findViewById(R.id.memofeedback);
holder.reply = (Button) rowView.findViewById(R.id.buttonFeedback);
holder.accept = (Button) rowView.findViewById(R.id.buttonAccepteer);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText(commentarray.get(position));
holder.textView2.setText(datearray.get(position));
holder.textView3.setText(fromarray.get(position));
holder.textView4.setText(feedback.get(position));
if (holder.textView4.getText().toString().equals("")
|| holder.textView4.getText().toString().equals(" ")) {
holder.textView4.setText(" - ");
}
// ONCLICKLISTENER FOR REPLYIMAGE
holder.reply.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog = new Dialog(NostradamusActivity2.parentcontext);
dialog.setContentView(R.layout.memo_dialog);
dialog.setTitle("Feedback");
feedbacktxt = (EditText) dialog.findViewById(R.id.memoeddittxt);
Button cancel = (Button) dialog
.findViewById(R.id.memobtncancel);
// CANCEL BUTTON ONCLICKLISTENER REPLYIMAGE
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
Button okay = (Button) dialog.findViewById(R.id.memobtnokay);
// OKAY BUTTON ONCLICKLISTENER REPLYIMAGE
okay.setOnClickListener(new OnClickListener() {
private String errormessage;
private AlertDialog alertDialog;
public void onClick(View v) {
Database2 db = new Database2(
"https://"
+ website
+ "/index2.php?option=com_webservices&controller=json&method=core.memos.memo_feedback&token=",
"token", token, "id", ids.get(position),
"memo_feedback", feedbacktxt.getText()
.toString());
}
// //ACCEPTEER MEMO BUTTON ONCLICKLISTENER
holder.accept.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
NostradamusActivity2.parentcontext);
builder.setMessage("Heeft u de opdracht uitgevoerd?")
.setCancelable(false)
.setPositiveButton("Ja",
new DialogInterface.OnClickListener() {
private AlertDialog alertDialog;
public void onClick(DialogInterface dialog,
int id) {
Database2 dbaccept = new Database2(
"http://intern.koeckers.nl/index2.php?option=com_webservices&controller=json&method=core.memos.memo_state&token=",
"token", token, "id", ids
.get(position),
"memo_completed", "1");
dialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("Nee",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
return rowView;
}
}
You are getting the item by ids.get(position).
You are using the arrayList, instead use the getTag() method.
rowView.getTag() will give you the ViewHolder from which, you can get the correct ids and Feedback Text.
Database2 dbaccept = new Database2(
"http://intern.koeckers.nl/index2.php? option=com_webservices&controller=json&method=core.memos.memo_state&token=",
"token", token, "id", ids.get(position),
"memo_completed", "1");
I'm creating a custom Dialog that is started by a custom spinner. What I was trying to do is customize the dialog the spinner calls. However, there is an annoying space in the dialog. I've tryied all my resources to fix it, but nothing. I also followed this question's answer but didn't solve.
In the spinner xml file I pass it like this. It references the following class named CustomSpinner, that extends a Spinner:
<com.myproject.CustomSpinner
android:id="#+id/customSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="13dp"
android:prompt="#string/my_spinner"/>
And I have this class that is my custom dialog class:
public class CustomSpinnerDialog extends Dialog implements OnItemClickListener, View.OnClickListener
{
private OnItemSelectedListener onItemSelectedListener;
public DialogInterface.OnClickListener mListener;
public Context mContext;
public interface OnItemSelectedListener
{
public void onItemSelected(String itemValue);
}
public CustomSpinnerDialog(Context context, CustomSpinner.SpinnerAdapter spinnerAdapter, DialogInterface.OnClickListener listener)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.custom_spinner);
mListener = listener;
mContext = context;
ListView listView = (ListView) this.findViewById(R.id.listview);
listView.setAdapter(spinnerAdapter);
listView.setOnItemClickListener(this);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
public void setOnItemSelectedListener(OnItemSelectedListener listener)
{
this.onItemSelectedListener = listener;
}
#Override
public void onClick(View v)
{
if(mListener != null)
mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(mListener != null)
mListener.onClick(this, position);
String text = (String) parent.getItemAtPosition(position);
onItemSelectedListener.onItemSelected(text);
}
public void setDialogTitle(String title)
{
TextView titleText = (TextView) this.findViewById(R.id.titleText);
titleText.setText(title);
}
}
And this is my custom spinner:
public class CustomSpinner extends Spinner implements DialogInterface.OnClickListener
{
public Context mContext;
public String[] mDataList;
public CustomSpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
this.mContext = context;
}
#Override
public boolean performClick()
{
boolean handled = false;
if (!handled)
{
handled = true;
CustomSpinnerDialog dialog = new CustomSpinnerDialog(mContext, (ListAdapter) getAdapter(), this, R.style.FullHeightDialog);
dialog.setDialogTitle(mContext.getResources().getString((R.string.my_dialog_text)));
dialog.show();
}
return handled;
}
#Override
public void onClick(DialogInterface dialog, int which)
{
setSelection(which);
dialog.dismiss();
}
}
My Spinner is created like this in an Activity:
cSpinner= (CustomSpinner) findViewById(R.id.customSpinner);
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);
Here is a screenshot of how the dialog looks like:
diaPhoto = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
I solved it following the documentation
And based in this code as advised by the documentation link above:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();