I'm using a custom ListView with a Title and a Subtitle where you can read a brief explanation of the item.
For each item on the list, I'm displaying an AlertDialog to select an option (different for each case). When the option is selected, i want to change the Subtitle for the option selected by the user.
This is what I've tried:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
case 1:
final CharSequence[] alertText2 = {"1", "2", "3", "5", "10", "20", "60"};
ventana.setTitle("Max. duration");
ventana.setItems(alertText2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText2[item]);
}
});
ventana.show();
break;
case 2:
final CharSequence[] alertText3 = {"3", "5", "10", "20", "30", "60"};
ventana.setTitle("Time between events");
ventana.setItems(alertText3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText3[item]);
}
});
ventana.show();
break;
For the first item on the list it works fine, when I select an option, the subtitle get replaced by that option, but when I make a selection in the AlertDialogs of the other 2 items, the option selected replaces the subtitle of the first item!
Any idea of how can I fix that?
Since nobody answered the question and i find a solution, im going to publish it here to help other people who eventually can be facing the same problem or a similar one :D
I just remove the TextView subTitulo = (TextView) findViewById(R.id.subTitulo); from each case and added it before the switch starts but "taking" the view argument on the onClick function (the type final, is because Eclipse warned me about it :P) : final TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
The code looks like this:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
switch(position){
case 0: final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
[...]
Related
I have a class that extends baseadapter to assign values to two textviews, I then call this class in the main activity and initialise it with the values for the two textviews. I then set the adapter class to a listview.
What I want to do when the user clicks on the list a AlertDialog with a edittext pops up and the user can enter a new value and append that back to the listview. I'm having trouble implementing this, please help.
EDIT
This method shows the alertdialog
public void editInfo(final String item, final List<Info> list, final int position, final String itemTitle, final InfoAdapter adapter)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit "+item+"?");
//Create a Layout where all the views will be appened
LinearLayout myLayout = new LinearLayout(this);
myLayout.setOrientation(LinearLayout.VERTICAL);
// Set an EditText view to get user input
final EditText inputEdit = new EditText(this);
//Declare all the properties for the email inputBox
inputEdit.setHint(item);
inputEdit.setSingleLine(true);
myLayout.addView(inputEdit);
// add all the EditTexts into the alertdialog view
alert.setView(myLayout);
//Declare the variable that will be returned
alert.setPositiveButton("Save", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
//Get all the values from the input boxes
editItem = inputEdit.getText().toString();
//Validate all the data before doing anything with it
if(editItem.equals(""))
{
showEnterValuesToast();
editInfo(item,list,position,itemTitle,adapter);
}else
{
list.set(position, new Info(itemTitle, item));
adapter.notifyDataSetChanged();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
This is the listView's setOnItemClickListener
final InfoAdapter adapter = new InfoAdapter(this, listOfInfo);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
switch(position)
{
case 0:
String itemToEditName = listOfInfo.get(position).getItem();
editInfo(itemToEditName,listOfInfo,position,"Name",adapter);
break;
case 1:
String itemToEditSurname = listOfInfo.get(position).getItem();
editInfo(itemToEditSurname,listOfInfo,position,"Surname",adapter);
break;
case 2:
String itemToEditEmail = listOfInfo.get(position).getItem();
editInfo(itemToEditEmail, listOfInfo, position,"Email",adapter);
break;
case 3:
String itemToEditDate = listOfInfo.get(position).getItem();
editInfo(itemToEditDate,listOfInfo,position,"Date Of Birth",adapter);
break;
case 4:
String itemToEditAddress = listOfInfo.get(position).getItem();
editInfo(itemToEditAddress,listOfInfo,position,"Physical Address",adapter);
break;
case 5:
String itemToEditPostal = listOfInfo.get(position).getItem();
editInfo(itemToEditPostal,listOfInfo,position,"Postal Code",adapter);
break;
case 6:
String itemToEditNumber = listOfInfo.get(position).getItem();
editInfo(itemToEditNumber,listOfInfo,position,"Phone Number",adapter);
break;
}
}
});
listView.setAdapter(adapter);
I am currently having issues accessing public array list in an setPositiveButton onClick function. accessing it with this doesn't work. Here is my code:
Declaring the array list with title modules.
public class DisplayModulesActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
ArrayList<Modules> modules;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_modules);
modules = new ArrayList<Modules>();
modules.add(new Modules("387COM Smartphone App Development"));
ListView listView = (ListView) findViewById(R.id.moduleList);
adapter = new ArrayAdapter<Modules>(this,
android.R.layout.simple_list_item_1, modules);
listView.setAdapter(adapter);
registerForContextMenu(listView);
listView.setOnItemClickListener(this);
}
}
I want to access the array list here and remove something from it (line with modules.remove(i); it does not work.
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == R.id.delete_module) {
AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
confirmDel
.setMessage("Are you sure you want to delete this module and all its contents?");
confirmDel.setCancelable(true);
confirmDel.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface,
int i) {
Log.v("Test", "Confirm Delete YES pressed");
// AdapterView.AdapterContextMenuInfo info =
// (AdapterView.AdapterContextMenuInfo)
// item.getMenuInfo();
// int i = info.position;
modules.remove(i);
// adapter.notifyDataSetChanged();
}
});
confirmDel.setNegativeButton("No", null);
confirmDel.show();
}
}
Any ideas please?
FIX
public boolean onContextItemSelected(final MenuItem item)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final int arrayItem = info.position;
if(item.getItemId() == R.id.delete_module)
{
AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
confirmDel.setMessage("Are you sure you want to delete this module and all its contents?");
confirmDel.setCancelable(true);
confirmDel.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.v("Test", "Confirm Delete YES pressed");
modules.remove(arrayItem);
adapter.notifyDataSetChanged();
}
});
confirmDel.setNegativeButton("No", null);
confirmDel.show();
}
You problem is, that i is not the position of the item in the ArrayList modules.
It is the position of the positive button (which might be 0 or 1 or 2 on every click, no matter which icon; maybe it's -1).
To fix this problem do the following:
Save/use the position given in onItemClick(AdapterView<?> parent, View view, int position, long id) and somehow pass it through to the onClickListener() of the dialog. Use this position to remove an item from your modules list.
E.g. in onItemClick() save position to a filed of your class mClickedPosition and call modules.remove(mClickedPosition) in the listener.
just add this in your code:
adapter = new ArrayAdapter<Modules>(this,android.R.layout.simple_list_item_1,modules);
listView.setAdapter(adapter);
do this ;)
public boolean onContextItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.delete_module)
{
AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
confirmDel.setMessage("Are you sure you want to delete this module and all its contents?");
confirmDel.setCancelable(true);
confirmDel.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
Log.v("Test", "Confirm Delete YES pressed");
//AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//int i = info.position;
modules.remove(i);
adapter = new ArrayAdapter<Modules>(this,android.R.layout.simple_list_item_1,modules);
listView.setAdapter(adapter);
}
});
confirmDel.setNegativeButton("No", null);
confirmDel.show();
}
I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this :
Give Me a help :
I was using this.... to add multiple buttons
alertDialog.setButton(delete, "Delete", new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
but I found..., change setButton() to setButton2().. something like..... wt xcan i do for this....
A simple solution without xml:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setItems(new CharSequence[]
{"button 1", "button 2", "button 3", "button 4"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
Toast.makeText(context, "clicked 1", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
break;
}
}
});
builder.create().show();
I would inflate the AlertDialog with my own custom view (my_alert_dialog.xml).
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root));
Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();
You can only create a Alertdialog with 3 buttons if you dont make the view by yourself.
You can either make your own custom view in xml.
but i'd suggest you just make a List.
Check
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
"Adding a list"
Dialog dialog = new Dialog(context);
RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null);
dialog.setContentView(featureLayout);
dialog.show();
int item = 0;
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
item = -1;
for (int i = 0; i < list.size(); i++) {
item++;
intList.add(i);
}
showRatingBarAlertDialog(intList);
private void showRatingBarAlertDialog(ArrayList<Integer> Id) {
if (item != -1) {
RatingFragment alertDialog = RatingFragment.instance(BaseActivity.this, Id.get(item), (ratingValue, comments) -> {
CXLog.d(TAG, "select the rating::" + ratingValue);
CXLog.d(TAG, "comment the feednback " + comments);
item--;
showRatingBarAlertDialog(requestId);
});
alertDialog.show(CXBaseActivity.this.getFragmentManager(), "alertDialog");
}
}
I'm new to this forum, but i came here because i need some help with some spinners i'm trying to create for an Android app.
I have created 1 spinner and that works fine, but now i want to add a second spinner and i want the if statement i have to work depending on the two selections of the spinners.
For example, if item 1 is selected on spinner 1 and item 3 is selected on spinner 2 then do the if statement.
But i don't know how to get that to work. could anyone help me please.
This is the code i have now for 1 spinner:
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.weight_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id)
{
final String itemSelected = parent.getItemAtPosition(pos).toString();
if (valueEntered.getText().length() == 0)
{
valueEntered.setText(String.valueOf(0));
}
if (itemSelected.equals("Stones"))
{
float valueInput = Float.parseFloat(valueEntered.getText().toString());
Toast.makeText(parent.getContext(), "The scale is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
valueEntered.setText(String.valueOf(convertSToK(valueInput)));
}
}
I really need some help, many thanks,
Davide Sousa
Use a dialog like the spinner:
private Dialog b1()
{
final String[] items = {
"Item 1",
"Item2",};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Hi this is a spinner"));
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item) {
case 0:{
/* Item 1 */ break;}
case 1:{
/* Item 2 */break;}
}
}
});
return builder.create();
}
and then show the dialog with b1().show();
I am new to Android. I am stuck, need help.
I have a List as my Activity. OnItemLongClickListener for any element of a List, I am displaying a Dialog with Custom List (Red, Green, Blue) & on the selection of any of the items (Red, Green, Blue) I need to change the back color of the selected item (on which the event raised the Dialog) of a List (main activity).
The dialog box pops, but I am stuck how to get the selected item (of Dialog). Below is my code.
public class SimpleList extends ListActivity
{
String[] contactNames = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"};
ArrayAdapter<String> contactAdpater;
String itemSelected;
String choosenColor;
private final Context context = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> myContactList = new ArrayList<String>(Arrays.asList(contactNames));
OnItemLongClickListener itemChangeColorListener = new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1, int position, long arg3) {
itemSelected = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final String[] colorNames = {"Red","Green","Blue"};
builder.setTitle("Pick a Colour!")
.setItems(colorNames, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
choosenColor = colorNames[which];
//Toast.makeText(getApplicationContext(), colorNames[which], Toast.LENGTH_SHORT).show(); <<-- its working fine here
//I am not able to access parent here... I want to perform,
**//parent[position].setBackgroundColor(Color.RED); in case Red is selected from Dialog**
}
});
builder.show();
return false;
}
};
contactAdpater = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
myContactList);
setListAdapter(contactAdpater);
getListView().setOnItemLongClickListener(itemChangeColorListener);
}
}
Yes got your problem
You have to use view's setBackgroundColor(int color); for changing the color if selected item of first ListView with the color selelcted from Dialog.
So In onClick you must use :
choosenColor = colorNames[which];
if(choosenColor.equals("Red"))
{
view.setBackgroundColor(Color.RED);
}
else if(choosenColor.equals("Blue"))
{
view.setBackgroundColor(Color.BLUE);
}
else if(choosenColor.equals("Green"))
{
view.setBackgroundColor(Color.GREEN);
}