Below inside my adapter class as i will get data inside button click i want to populate spinner inside button click and set item selected listener in it.
tvMediaCategory.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
for(int i=0; i<mediaList.get(position).getMediaCatList().size(); i++)
{
catArr[i] = mediaList.get(position).getMediaCatList().get(i).getCategoryName();
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for(int i=0; i<catArr.length; i++)
{adapter.add(catArr[i]);}
adapter.add("HINT_TEXT_HERE"); //This is the text that will be displayed as hint.
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
Toast.makeText(context, "position clicked "+position, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
tvMediaCategory.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
spinner.performClick();
//showCategoryDlg(catArr, position, selectedPos, tvMediaCategory);
}
}
});
here my spineer not getting open but selecting default item. how to make it work. this my code is inside a adapter. how can i show my spinner items on click of button?
Try this example... i am sure you will be able to get what u r looking for....
you need to implement the onItemSelectedListener
https://www.mkyong.com/android/android-spinner-drop-down-list-example
Related
i have two spinners, city and neighborhood, when i select a city it populates neighborhoods list. but i want when i click neighborhood spinner and city is empty to show a message or an Alertdialog.
i tried to use ontouchevent on the second spinner but it doesn't work.
i m using custom spinner com.toptoche.searchablespinnerlibrary.SearchableSpinner thanks for help
neighborhoodSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Toast.makeText(getApplicationContext(),"city emty",Toast.LENGTH_LONG).show();
}
return false;
}
});
why don't you use onClickListener instead?
s.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
cityspinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemcity = parent.getItemAtPosition(position).toString();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
neighborhoodSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItemneighbour = parent.getItemAtPosition(position).toString();
if(selectedItemcity.equals("your item name")) //Also you can check previous spinners position here if 0 then show popup or dilaog
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});
Use setOnItemSelectedListener to get spinner item selection listener.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
// Write your code here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
It depends whether your spinner has a default value to mean nothing.
For example, if your spinner has a city value of "Please select a city" or something like that, which doesn't have to be the case.
Your spinner may already have a city selected when it's populated.
In the case that "Please select a city" is an option, this code might help:
Spinner city= findViewById(R.id.spinner_city);
Spinner neighborhood = findViewById(R.id.spinner_neighborhood);
neighborhood.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(city.getSelectedItem().toString().equals("Please select a city")){
Toast.makeText(MyActivity.this, "Please select a city first", Toast.LENGTH_SHORT).show();
}else{
// do nothing
}
}
});
I have a listview which uses this layout:
android.R.layout.simple_list_item_checked
I have an OnItemClickListener that gets the check status of the clicked item and either checks or unchecks that item but it does not work. There are NO errors in logcat. When I click on an item in the listview simply nothing happens.
The OnItemClickListener looks like this:
//------------------- OnItemClickListener -----------------------------
lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView textview = (CheckedTextView)view;
if (!textview.isChecked()){
textview.setChecked(true);
}else {
textview.setChecked(false);
}
}
});
Try with this...
//sample code
TextView TxtName = (TextView) findViewById(R.id.NameControlId);
TxtName.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.NameControlId:
Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_SHORT).show();
break;
}
}
I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}
In my android app I have two List Views and two buttons.On clicking first button, first List View will be visible and on second button second List View will be visible.In one List View I will have list of items and in second List View there will be ADD button. Now clicking on ADD button my first List View will be visible and user can select items to add in second List View. I have done it successfully. But my problem is, by clicking on second button List View is appearing but there are no data which are added from First List View. Below is my code.
What am I missing?
final ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
final View header = getLayoutInflater().inflate(R.layout.footer, null);
lv.addFooterView(header, null, false);
btnCollege.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String listItems[] = {};
final ArrayAdapter<String> string = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1);
lv.setAdapter(string);
Button btnAdd = (Button) header.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (j == 0)
{
lv.setVisibility(View.VISIBLE);
header.setVisibility(View.VISIBLE);
j = 1;
}
else if (j == 1)
{
lv.setVisibility(View.INVISIBLE);
header.setVisibility(View.INVISIBLE);
j = 0;
}
else {}
lv1.setVisibility(View.VISIBLE);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
String selected = lv1.getItemAtPosition(arg2).toString();
string.add(selected);
string.notifyDataSetChanged();
setListAdapter(string);
}
});
}
});
}
});
The view is not getting updated. use handlers for updating listview. That would help you solve the issue
I have a view object that contains a edittext, an invisible spinner, and a button. I load the view via the LayoutInflater and it shows up and everything works fine. I hit the button, the spinner list shows up and I select the item that I want. My problem is that the setOnItemSelectedListener for the spinner doesn't fire so I can't set the edittext to the selected value.
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, listItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spn = (Spinner)sEdit.findViewWithTag("spn"+fieldID);
final String spnHolder = "spn"+fieldID;
spn.setAdapter(adapter);
ImageButton bSpn = (ImageButton)sEdit.findViewWithTag("btn"+fieldID);
bSpn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
curVw = (EditText)sEdit.findViewWithTag(v.getTag().toString().split("btn")[1]);
((Spinner)sEdit.findViewWithTag(spnHolder)).performClick();
}
});
try {
spn.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
curVw.setText(((TextView)arg1).getText().toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
catch (Exception ex)
{
ex.printStackTrace();
}
I added a Try...Catch to see if it was just failing past that point, but it is never called.
Instead of using SetOnclickListener please use:
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
//implements methods and perform your action. This will works definately
};