Hi I am using AlertDialog with no custom views, setting array adapter of lists by using:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setTitle("Please Select");
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
builder.setSingleChoiceItems(arrayAdapter, 0, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
builder.show();
and I want to attach EditText for searching list, But EditText shows at the end of list. I want EditText top of list, so please suggest me without using custom layouts.
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
I tried by changing sequence of builder.setView(input);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
LinearLayout layout=new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(input);
builder.setTitle("Please Select");
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
layout.addView(list);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
builder.setView(layout);
builder.show();
Try this
boolean checked = false;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LinearLayout linear = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams parm = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linear.setOrientation(LinearLayout.VERTICAL);
linear.setLayoutParams(parm);
final EditText input = new EditText(MainActivity.this);
builder.setTitle("Please Select");
linear.addView(input);
String[] listStrings = {"string1", "string2"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, listStrings);
ListView list=new ListView(this);
list.setAdapter(arrayAdapter);
linear.addView(list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckedTextView chk = (CheckedTextView) view;
Log.d("", "Checked = " + checked);
checked = checked ? false:true;
chk.setChecked(checked);
}
});
builder.setView(linear);
builder.show();
Related
I have a dialog with a spinner. Currently, the dialog and the spinners works fine. However, the spinner doesn't close after I selected an item. I need it to be close and return to the activity after an item is selected. Thanks.
Here's the code for my dialog.
String[] s = {"A", "B", "C", "D", "E", "F" };
final ArrayAdapter<String> adp = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, s);
final Spinner sp = new Spinner(getActivity());
//sp.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
Log.d("selectedItem:", selectedItem);
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(sp);
builder.create().show();
There is no method on Spinner to close it,when spinner item is selected, and that will close your spinner, add to your setOnItemSelectedListener
sp.setSelection(int position)
and then just change your AlertDialog constructor a bit, put alert.dismiss(); to dismiss the AlertDialog after user selects an item in your spinner
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(sp);
final AlertDialog alert = builder.create();
alert.show();
Try this
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final Spinner sp = new Spinner(getActivity());
sp.setAdapter(adp);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
Log.d("selectedItem:", selectedItem);
builder.dismiss();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
builder.setView(sp);
builder.create().show();
I'm trying to add a two spinners inside a dialog (popup). The problem I'm having is populating the spinners. I get no error I can see, and basically the same code works if It's in a tab-fragment, and not the dialog.
This is the code that does not popluate the spinners inside the dialog.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
League league;
league = ((LeagueMainActivity)getActivity()).getLeague();
View v = inflater.inflate(R.layout.diaglog_add_match, null);
Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner_dialog_player1);
Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_dialog_player2);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
spinner2.setAdapter(adapter);
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
.setTitle("Add match")
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//LoginDialogFragment.this.getDialog().cancel();
/* do I really need to do anything??? */
}
});
AlertDialog dialog = builder.create();
return dialog;
}
This is the code that works within a (tabbed) fragment:
public class UnnamedFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_unnamed, container, false);
Spinner spinner1 = (Spinner) rootView.findViewById(R.id.spinner);
String [] items = {"test 1", "test 2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Log.d("Spinner: ", "" + spinner1);
spinner1.setAdapter(adapter);
return rootView;
}
}
Okay so what I did wrong was inflating/creating two independent views with the same context.
First I did:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
And then:
builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
So I set the view for the builder as a new view, and not the same ones I used for the spinner.
So instead if I do:
View v = inflater.inflate(R.layout.diaglog_add_match, null);
builder.setView(v)
That does the trick.
I have this working piece of code for selecting a number from a GridView that's inside an AlertDialog:
public void iconSelect (View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
GridView gridview = new GridView(this);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1; i < 10; i++) {
mList.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
gridview.setAdapter(adapter);
gridview.setNumColumns(3);
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Chosen: " + position, Toast.LENGTH_SHORT).show();
}
});
builder.setView(gridview);
builder.setTitle("Icon selector");
builder.show();
}
My goal is that when the user selects a number from the gridview then the dialog get closed. Right now it lets the user click the number on the gridview and show the toast many times as the user wants until he presses back button.
What should I do?
I'm looking for a method similar to builder.close() or builder.dismiss() to be executed inside the listener. (Making builder a final variable)
You should use builder.show() or builder.create() to hold an instance of AlertDialog in order to dismiss it after that
Try the following code:
public void iconSelect (View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
GridView gridview = new GridView(this);
List<Integer> mList = new ArrayList<Integer>();
for (int i = 1; i < 10; i++) {
mList.add(i);
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mList);
gridview.setAdapter(adapter);
gridview.setNumColumns(3);
builder.setView(gridview);
builder.setTitle("Icon selector");
final AlertDialog dialog = builder.show();
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Chosen: " + position, Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
I'm new to Android Sqlite DB Dev't so I need your help guys. I was able to do the basic CRUD operations. Now I want to know how to refresh the listview to display the newly added data. I've looked into adapter.notifyDataSetChanged but I wonder why it's not working for me. Here's what I've tried so far
private void initControls() {
SearchAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] SearchItems = getResources().getStringArray(R.array.DogSearch);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.searchitems,SearchItems);
SearchAuto.setAdapter(adapter);
SearchAuto.setThreshold(0);
search = (Button) findViewById (R.id.handle);
search.setOnClickListener(this);
lv = (ListView) findViewById (R.id.lv);
values = dbHelper.getAllAnimals(ANIMALTYPE);
for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}
// TODO onItemClick
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String strDog = parent.getItemAtPosition(position).toString();
Intent i = new Intent(DogClass.this, Dogs.class);
i.putExtra("dog_name", strDog);
startActivity(i);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addanimal, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// get prompt_addprayer.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt_addanimal, null);
AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText etBreed = (EditText) promptsView
.findViewById(R.id.etBreed);
final EditText etDescription = (EditText) promptsView
.findViewById(R.id.etDescription);
final EditText etDiet = (EditText) promptsView
.findViewById(R.id.etDiet);
final EditText etShelter = (EditText) promptsView
.findViewById(R.id.etShelter);
final EditText etHygiene = (EditText) promptsView
.findViewById(R.id.etHygiene);
final EditText etMedication = (EditText) promptsView
.findViewById(R.id.etMedication);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
String strAnimalType = ANIMALTYPE;
String strBreed = etBreed.getText().toString();
String strDesc = etDescription.getText().toString();
String strDiet = etDiet.getText().toString();
String strShelter = etShelter.getText().toString();
String strHygiene = etHygiene.getText().toString();
String strMedication = etMedication.getText().toString();
dbHelper.addAnimalInfo(strAnimalType, strDesc,
strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
lvAdapter.notifyDataSetChanged();
}
})
.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();
return true;
}
Any ideas? I really need to get this working. Thanks
dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
lvAdapter.notifyDataSetChanged();
This is where you lacked. What you did is, you just inserted data into database, still you are remaining to bring data from database and bind it with adapter, and after that you may call notifyDataSetChanged();, and you'll achieve what you want...
Try this:
dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
values = dbHelper.getAllAnimals(ANIMALTYPE);
lvAdapter.clear();
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lvAdapter.notifyDataSetChanged();
Note: you should not write this way. Your code:
for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}
If your getAllAnimals() method is returning an arrayList then you simply need to:
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
The last argument of your ArrayAdapter constructor needs List<Item> to be passed. And then just call setAdapter(adapter) of ListView.
For more clarification, refer this SO link: notifyDataSetChanged example
http://androidadapternotifiydatasetchanged.blogspot.in
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.
}
});