i was searching and trying alot today but it all does not help.
I want to use a Dialog with my own layout. In this layout there is a spinner with some subitems. My Problem is i can't get those selecteditems.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.decision, null));
LayoutInflater inflater2= getLayoutInflater();
View viewInflater=inflater2.inflate(R.layout.decision, null);
Spinner spinner=(Spinner) viewInflater.findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("click", "returnValue");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.v("click", "nothing");
}
});
builder.setPositiveButton("Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "positive");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v("click", "negative");
}
});
builder.show();
}
I think that the problem can be that you inflate a layout, which returns a new view. This new view has different reference than this original and when you set the listener, it affects only at this new, not this you want. Maybe you should set this new view to newly created Alert Dialog?
Have you tried something like this (instead of builder.show())?
AlertDialog alertDialog = builder.create();
alertDialog.setView(viewInflater);
alertDialog.show();
It helped me so much when I wanted do set my custom view to alertdialog.
Related
I have ArrayList of object (citiesInSpinner) each object have two value(Id, Name)
I have already get it in alert dialog
I use this function to alert dialog:
public void test()
{
FillSpinner();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SearchFligtsActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.custom, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Select City");
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,citiesInSpinner);
lv.setAdapter(adapter);
/* alertDialog.setItems(citiesInSpinner, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
/////
}
});*/
alertDialog.show();
/**/
}
now I want to make something to get the ((ID)) of item (I mean the Id of City)
I tried to do that but I failed...
any help Please !!
and thank you
Simply use
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
City city = citiesInSpinner.get(position)
//get your id -> city.Id
}
});
I have created a Custom AlertDialog and I'm trying to set text for EditTexts. I have tried following
public class CheckListDialog extends DialogFragment {
private View view;
public CheckListDialog(View view) {
this.view = view;
}
#Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_checklist, null);
EditText etCost = (EditText) dialogLayout.findViewById(R.id.etCost);
EditText etOdoReading = (EditText) dialogLayout.findViewById(R.id.etOdometer);
etOdoReading.setText("bla");
etCost.setText("tada");
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(Reminders.this, android.R.color.transparent));
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
.setTitle("jaja")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
The EditTexts are empty and does not contain the values. How can i fix this?
Replace
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
with
builder.setView(dialogLayout)
You're modifying the edittexts in one layout and then inflating a new layout for the dialog.
For such operations you have to use a custom layout to inflate into the CheckListDialog class of yours. DialogFragment will provide you complete freedom to customize your dialog as per your requirement.
You can refer to this tutorial
I have a ListView with each row consisting of a textview and a checkbox.
The user is allowed to click on each TextView, and once he does a DialogBox is presented to him where he is expected to either choose Yes or No.
When he chooses No, the CheckBox next to the TextView the user clicked needs to be ticked automatically.
The problem that I am facing is that no matter which TextBox the user chooses, the last CheckBox of the list gets ticked all the time, and not the one next to the chosen TextView.
After debugging I noticed that this is happening because the getView() method of the adapter class stops on the last row, thus the last row always gets selected. I tried setting up tags for each row so that I can than tick the checkbox accordingly but then neither Checkboxes get ticked.
I would appreciate any help on this matter.
....
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.text.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(final View v)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity)mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//holder.chk.setChecked(true); // This ticks the last checkbox of the list.
if(holder.chk.getTag() == v.getTag())
{
holder.chk.setChecked(true);
}
}
});
builder.show();
}
});
Try passing the checkbox for the row into your on click listener:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.text.setOnClickListener(new MyClickListener(holder.chk);
}
private class MyClickListener implements OnClickListener
{
Checkbox checkbox = null;
public MyClickListener(final Checkbox checkbox)
{
this.checkbox = checkbox;
}
#Override
public void onClick(final View v)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Materials");
builder.setMessage("Did you require any materials to fix this error?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String clickedError;
clickedError = holder.text.getText().toString();
Intent intent = new Intent(mContext, Material.class);
intent.putStringArrayListExtra("materialList", materialList);
intent.putExtra("clickedError", clickedError);
intent.putExtra("repairID", repairID);
((Activity)mContext).startActivityForResult(intent, 1);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//checkbox.setChecked(true); // This ticks the last checkbox of the list.
if(checkbox.getTag() == v.getTag())
{
checkbox.setChecked(true);
}
}
});
builder.show();
}
});
CheckBox extends TextView. Use CheckBox.setText(CharSequence | int) to set text into CheckBox and remove the TextView (that will not solve your check problem, of course). Regarding your problem - it's because most AdapterView's re-use Views. Store the checked item information somewhere and in getView call setChecked(checkedItems.get(pos)).
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
holder.chk.setChecked(checkedItems.get(position))
.....
builder.setNegativeButton("No", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
//holder.chk.setChecked(true); // This ticks the last checkbox of the list.
if(holder.chk.getTag() == v.getTag())
{
holder.chk.setChecked(true);
checkedItems.put(position, true);
}
}
});
}
Where checkedItems is a SparseBooleanArray
Im desperately trying to get my listView to open an alert dialog or a normal dialog that is filled with information. I cant seem to get it to work. I want it to display different information aswell depending on which item on the list is clicked
public class learn_tab1 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
BASICLIST));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setCancelable(false);
dialog.setTitle("Instructions");
dialog.setIcon(R.drawable.bone_icon);
dialog.setMessage("test");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
}
}
});
}
Try new AlertDialog.Builder(learn_tab1.this).create(); instead of new AlertDialog.Builder(this).create().
I'm wondering how it ever get complied....
EDIT
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
// ...
dialog.setMessage(item);
// ...
}
When displaying custom dialog box which is showing textView and EditView. How can i access these elements within dialog. Below is the code which is giving error.
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
string = uetd.getText().toString() + ":" + petd.getText().toString(); /////producing error
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
public class listdemo extends ListActivity {
/** Called when the activity is first created. */
private static final int DIALOG_TEXT_ENTRY = 7;
EditText uetd;
EditText petd;
SharedPreferences.Editor editor;
String string;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
uetd=(EditText)findViewById(R.id.username_edit);
petd=(EditText)findViewById(R.id.password_edit);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
showDialog(DIALOG_TEXT_ENTRY);
}
});
}
}
Instead of returning the dialog immedately with return new AlertDialog.builder....and so on....you should save an instance of the dialog by creating a class variable just like this
AlertDialog dialog;
Then you can easily access the views of the dialog within the callback of the dialog:
dialog.findViewById(R.id.username_edit);
You have to do this because when creating the callback it is in a way constructing a new class in memory so when you call findViewById() directly it can't find the activity
try this code
uetd=(EditText) textEntryView.findViewById(R.id.username_edit);
after following line in your code
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);