Hello guys i would like to display more than one item horizontally in a single list. This items are picked from the dialog box that has two button ok and cancel. When you click okay it should pick the values from the edit text and a string value from the dialog and display in the list horizontally.
Array List Adapter
public class VehicleListAdapter extends ArrayAdapter<String> {
private Context context;
private List<String> vehicle_no;
public VehicleListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.context = context;
this.vehicle_no = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the vehicle number we are displaying
String my_vehicle = vehicle_no.get(position);
//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.vehicle_num_layout, null);
TextView txt_vehicle_num = (TextView) view.findViewById(R.id.txt_vehicle_num);
txt_vehicle_num.setText(my_vehicle);
return view;
}
}
Activity with the list
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, 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.editTextDialogUserInput);
// 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
if(userInput.getText().toString().trim().length()==0) {
userInput.setError("Vehicle Number Required");
focus.start();
String stime = focus.getText().toString();
}
else {
result.setText(userInput.getText());
ListView list = (ListView) findViewById(R.id.list);
vehicle_list.add(userInput.getText().toString().trim());
VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
list.setAdapter(listAdapter);
}
}
})
.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();
}
});
First of all, why you are creating new listview every time when user hit ok of dialog. Remove this line ListView list = (ListView) findViewById(R.id.list); from dialog positive button click.
Add line as shown below after your code in positive button click
VehicleListAdapter listAdapter = new VehicleListAdapter(StartWatch.this, 0, vehicle_list);
list.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged(); // Add this line to tell adapter that data has been changed
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
}
});
When the user clicks the item of list View a alert Dialog box will be displayed to the user with options like "view","delete" or "update"
the list view item. Clicking on the list view item for the first time displays the alert Dialog with options but when i click another list View item or the same list View item the second time the app crashes.Thanks in advance.
Error :java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
//The main list displayed when the user log in successfully
ListView = (ListView) findViewById(R.id.list);
ListView2 = new ListView(this); //List to be displayed in the alert Dailog box
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long i = ListView.getItemIdAtPosition(position);
showAlertDailog();//The dialog box method
}
});
if(mCursorAdapter == null) //To check if the list is empty
{
TextView emptyTextView = (TextView) findViewById(R.id.empty);
emptyTextView.setText("No Notes");
ListView.setEmptyView(emptyTextView);
}else {
mCursorAdapter = new DataCursorAdapter(this, null);
ListView.setAdapter(mCursorAdapter);
}
public void showAlertDailog()
{
String [] items = {"View","Delete","Update"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_todo_2,R.id.textView4,items);
ListView2.setAdapter(adapter);
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(HomeActivity.this);
builder.setView(ListView2)
.setCancelable(false)
.setPositiveButton("Close",null);
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}
ArrayList<String> arrayList = new ArrayList<String>();
CharSequence[] animals = arrayList.toArray(new String[arrayList.size()]);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String selectedText = animals[item].toString();
}
});
AlertDialog alertDialogObject = dialogBuilder.create();
alertDialogObject.show();
This is simple way to display list inside alert dialog, where in arraylist just add string whatever you have to display inside alert dialog list.
It is exactly what you need
public class MainActivity extends Activity implements OnClickListener {
private Button mDoneButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDoneButton = (Button) findViewById(R.id.done_button);
mDoneButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final CharSequence[] items = {
"Rajesh", "Mahesh", "Vijayakumar"
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Do something with the selection
mDoneButton.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html
OUTPUT
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String[] listItems = { "Colour", "Font Size", };
if (listItems[position].equals("Font Size")) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Settings.this );
// set title
alertDialogBuilder.setTitle("Choose Font Size");
// 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
}
})
.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();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
});
public void showAlertDailog(Context context) {
ListView listView=new ListView(context);
yourLayout.LayoutParams params= yourLayout.LayoutParams(Yourlayout.LayoutParams.MATCH_PARENT,Yourlayout.LayoutPa
rams.MATCH_PARENT);
listview.setLayoutParams(params);
String [] items = {"View","Delete","Update"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
listView.setAdapter(adapter);
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
builder.setView(listView)
.setCancelable(false)
.setPositiveButton("Close",(dialog,something)->{dialog.dismiss()});
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();
}
OK so I am creating an ArrayAdapter and using it in my Alert Dialog because I don't want to show the default radio buttons on SingleItemSelection dialog.
Instead I want to change the background of the item that is selected, and then when the user presses the positive button I will perform the action related to the item that has been selected.
private void showAlertDialog()
{
final String[] options = getResources().getStringArray(R.array.dialog_options);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("My Dialog");
dialogBuilder.setAdapter(adapter, new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show();
// Here I need to change the background color of the item selected and prevent the dialog from being dismissed
}
});
//String strOkay = getString(R.string.okay);
dialogBuilder.setPositiveButton("OK", null); // TODO
dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
There are two problems I'm trying to tackle.
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
To prevent dialog from dismissing on item click you can use AdapterView.OnItemClickListener instead of DialogInterface.OnClickListener.
Like this:
dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});
You can set custom ListView as content of AlertDialog and set OnItemClickListener
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String[] items = ...;
ListView list = new ListView(this);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
...
}
});
builder.setView(list);
and then save reference to dialog
mDialog = builder.show();
in order to dismiss it if necessary
mDialog.dismiss();
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
Here is example
public class MainActivity extends AppCompatActivity {
private static final String listFragmentTag = "listFragmentTag";
private static final String data[] = {"one", "two", "three", "four"};
public MainActivity() {
super();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View v) {
ListFragment lf = new ListFragment();
lf.show(getSupportFragmentManager(), listFragmentTag);
}
public static class ListFragment extends DialogFragment {
#Override #NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("List")
.setItems(data, null)
.setPositiveButton("OK", null); // pass your onClickListener instead of null
// to keep dialog open after click on item
AlertDialog ad = adb.create();
ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
private int colorOrg = 0x00000000;
private int colorSelected = 0xFF00FF00;
private View previousView;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// restoring color of previous view
if(previousView != null) {
previousView.setBackgroundColor(colorOrg);
}
// changing items's BG color
view.setBackgroundColor(colorSelected);
previousView = view;
}
});
return ad;
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
}
You can use setCanceledOnTouchOutside(false) or setCanceleable(false).
Set selector for the root element tag of the dialog layout xml.
i'am having a list with textview, many data's are flowing in list and having one textview in xml....problem is i want to update the every textview entry in the list..i want to update the (TAG_QTY) textview in list when every value entry...
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_BARCODE, TAG_DIVISION, TAG_MRP,TAG_QTY}, new int[] {
R.id.txt, R.id.txt1, R.id.mrp,R.id.qty1 });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String name = ((TextView) view.findViewById(R.id.qty1)).getText().toString();
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
//final View textEntryView;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
fourth = (TextView)findViewById(R.id.qty1);
userInput = (EditText)promptsView.findViewById(R.id.editTextDialogUserInput);
//String ed = userInput.getText().toString();
//final int ed= Integer.parseInt(userInput.getText().toString());
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String ed = userInput.getText().toString().trim();
fourth.setText(ed);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
);
In order to get the list view child
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View c = lv.getChildAt(position);
// c is your list view child which is clicked
final TextView tv = (TextView) c.findViewById(R.id.qty1);
// tv is your textview of whom vwlue you have to change.
//changes the value of textview here and den notify data set changed and refresh the list.
}
});
I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.
private void buildDialog(){
int selectedItem = -1; //somehow get your previously selected choice
LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.listview, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
builder.setTitle("Select Weapon").setCancelable(true);
builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
ListView lv = ((AlertDialog) dialog).getListView();
itemId = lv.getAdapter().getItemId(which);
new changeEQ().execute();
}
});
dialog = builder.create();
}
This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.
EDIT: changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.
If you have a custom layout that you want to pass to your AlertDialog try:
LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);
If you want to define listeners try:
ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do as you please
}
});