I'm creating spinner programmatically spinner show perfectly dropdown but when I click ant item is not show Toast message when I debug is not go inside spinner click method what is wrong in my code? spinner item will show but click event not work what is wrong?
public class MainActivity extends Activity {
Spinner spinner;
String[] strings = { "Power Options", "Power off", "Restart" };
String[] subs = { "", "Shut phone down", "Close all apps and restart phone" };
int arr_images[] = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinner = new Spinner(this);
spinner.setAdapter(new MyAdapter(this, R.layout.row, strings));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l) {
// TODO Auto-generated method stub
if (i == 1) {
Toast.makeText(MainActivity.this, "PowerOff Perform",
Toast.LENGTH_SHORT).show();
}
if (i == 2) {
Toast.makeText(MainActivity.this, "Reboot Perform",
Toast.LENGTH_SHORT).show();
}
}
// If no option selected
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.row, parent, false);
TextView label = (TextView) row.findViewById(R.id.company);
label.setText(strings[position]);
TextView sub = (TextView) row.findViewById(R.id.sub);
sub.setText(subs[position]);
ImageView icon = (ImageView) row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == event.getKeyCode()) {
spinner.performClick();
return true;
}
return super.onKeyDown(keyCode, event);
}
maybe use setOnItemClickListener instead of setOnItemSelectedListener ?
Try to use
spinner.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> arg0, final View arg1, final int arg2, final long arg3) {
}
});
instead of
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l) {...
Related
In my application i have one Spinner and i should show some data from server into this Spinner.
My server data has :
"sections": [{
"id": 1,
"name": "Item 1"
}, {
"id": 2,
"name": "Item 2"
}, {
"id": 3,
"name": "Item 3"
}]
For this spinner i should set default text for show first time and when users click on spinners item show this items instead of default text.
For this i write below codes, but i con't know how can i set clickListener to this items for get id of each items!
My adapter code:
public class DashboardSupportSectionAdapter extends ArrayAdapter<Section> {
Context context;
List<Section> model;
String firstElement;
boolean isFirstTime;
public DashboardSupportSectionAdapter(Context context, int textViewResourceId, List<Section> model, String defaultText) {
super(context, textViewResourceId, model);
this.context = context;
this.model = model;
this.isFirstTime = true;
setDefaultText(defaultText);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (isFirstTime) {
model.get(0).setName(firstElement);
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = model.get(0).getName();
model.get(0).setName(defaultText);
}
public View getCustomView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row_dashboard_support_section, parent, false);
TextView label = row.findViewById(R.id.spinner_text);
label.setText(model.get(position).getName());
return row;
}
}
My Activity code:
public class DashboardCreateSupportActivity extends BaseActivity {
#BindView(R.id.dashboardCreateSupport_sectionSpinner)
Spinner dashboardCreateSupport_sectionSpinner;
String defaultTextForSpinner = "Select section";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_create_support);
ButterKnife.bind(this);
dashboardCreateSupport_sectionSpinner.setAdapter(new DashboardSupportSectionAdapter(this, R.layout.row_dashboard_support_section,
Constants.supportListResponse.getRes().getSections(), defaultTextForSpinner));
}
}
I want when click on each item, show id of item in toast message.
How can i it?
dashboardCreateSupport_sectionSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Constants.supportListResponse.getRes().getSections().get(position).getId();
}
});
dashboardCreateSupport_sectionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Section item = Constants.supportListResponse.getRes().getSections().getItem(position);
//use item object
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Use setOnItemSelectedListener.
I would even like to suggest to modify code like:
final DashboardSupportSectionAdapter dashboardSupportSectionAdapter =new DashboardSupportSectionAdapter(this, android.R.layout.simple_list_item_1,
sections, "default");
dashboardCreateSupport_sectionSpinner.setAdapter(dashboardSupportSectionAdapter);
and inside onItemSelected
Section item = dashboardSupportSectionAdapter.getItem(position);
I am trying to implement a simple listView with onClick functionality. On clicking the item in the listView, I want to show the name of the item in a 'toast message, but I am unable to do that. I have used the method suggested in this link and link but couldn't get the desired result. I have added my code below :
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new AppList();
final ListView userInstalledApps = (ListView)findViewById(R.id.appListView);
List<AppList> installedApps = getInstalledApps();
CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name);
Toast.makeText(getApplicationContext(),
"Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();
}
});
}
CustomAppAdapter.java
public class CustomAppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
Context context;
public CustomAppAdapter(Context context, List<AppList> listStorage) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listStorage = listStorage;
}
#Override
public int getCount() {
return listStorage.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
return convertView;
}
static class ViewHolder{
TextView textInListView;
ImageView imageInListView;
}
}
Below is the screenshot of the listView :
On trying this method:
Toast.makeText(getApplicationContext(),
"Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();
Suppose I clicked on the 2nd item in the listView so I get a toast message as Clicked 2. But I want that it shows me Clicked PhonePe
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new AppList();
final ListView userInstalledApps = (ListView)findViewById(R.id.appListView);
List<AppList> installedApps = getInstalledApps();
CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name);
Toast.makeText(getApplicationContext(),
"Clicked "+ installedApps.get(i).getName(),Toast.LENGTH_LONG).show();
}
});
}
Try this.
I have three different Spinner objects (two of them have a custom layout, one - spMode - is standard).
When the OkayButton is pressed, a Toast should be displayed with 3 Numbers: the SpinnerItemNumber of Mode, Color1 and Color2. But Mode is 0 each time the button is pressed. The others work fine. Just Mode is showing that item 0 is selected even if I select Item 2 or 3 in the App.
Here is the relevant code; any ideas why?
public class NewTimer extends Activity implements OnItemSelectedListener,OnClickListener
{
Button btnOkay;
EditText etTime;
int mode, minutes, color1, color2;
String[] strings = {"","",
"", "", "", ""};
int arr_images[] = { R.drawable.yellow,
R.drawable.orange, R.drawable.red,
R.drawable.blue, R.drawable.green, R.drawable.white};
public void onClick(View v)
{
if (v == btnOkay)
{
Toast.makeText(v.getContext(), Integer.toString(mode) + Integer.toString(color1) + Integer.toString(color2), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newtimer);
Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor1.setOnItemSelectedListener(this);
Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor2.setOnItemSelectedListener(this);
etTime = (EditText) findViewById(R.id.etTime);
btnOkay = (Button) findViewById(R.id.btnOkay);
btnOkay.setOnClickListener(this);
Spinner spMode = (Spinner) findViewById(R.id.spMode);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spMode_items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMode.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent,
View v, int pos, long id)
{
switch (parent.getId())
{
case R.id.spMode:
mode = pos;
break;
case R.id.spColor1:
color1 = pos;
break;
case R.id.spColor2:
color2 = pos;
break;
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
public class MyAdapter extends ArrayAdapter
{
public MyAdapter(Context context, int textViewResourceId,
String[] objects)
{
super(context, textViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=getLayoutInflater();View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.color);
label.setText(strings[position]);
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
return row;
}
}
}
You never set the setOnItemSelectedListener for that spinner:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newtimer);
Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor1.setOnItemSelectedListener(this);
Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
spColor2.setOnItemSelectedListener(this);
etTime = (EditText) findViewById(R.id.etTime);
btnOkay = (Button) findViewById(R.id.btnOkay);
btnOkay.setOnClickListener(this);
Spinner spMode = (Spinner) findViewById(R.id.spMode);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spMode_items, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spMode.setAdapter(adapter);
spMode.setOnItemSelectedListener(this); }
public void onItemSelected(AdapterView<?> parent,
View v, int pos, long id) {
switch (parent.getId())
{
case R.id.spMode:
mode = pos;
break;
case R.id.spColor1:
color1 = pos;
break;
case R.id.spColor2:
color2 = pos;
break;
} }
I am using a Listview. before implementing OnLongClick, my onListItemClick was working perfectly, however now, after implementing OnLongClick the long clicks work and normal list clicks don't do anything. It seems to hide exposure to the onListItemClick() function you already have working
can anyone see why/ suggest a solution?
public class CombChange extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ListEdit(this, symbols));
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selectedValue = (String) getListAdapter().getItem(position);
if (lastPressed.equals(selectedValue) ){
count++;}
}
public class ListEdit extends ArrayAdapter<String> implements OnLongClickListener{
private final Context context;
private final String[] values;
public ListEdit(Context context, String[] values) {
super(context, R.layout.activity_comb_change, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_comb_change, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values[position]);
rowView.setOnLongClickListener(new OnLongClickListener(){
public boolean onLongClick(View arg0) {
context.startActivity(new Intent(context,RestoreOriginal.class));
return false;
}
});
// Change icon based on name
String s = values[position];
if (s.equals("a")) {
imageView.setImageResource(R.drawable.a);
return rowView;
}
}
I think you shouldn't do rowView.setOnLongClickListener.
Try something likes this:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
// whatever you wanna do
return true;
}
});
I took the code from how to capture long press event for Listeview item of a ListActivity?
Hope this helps.
i m developing an application in which gridview contain list of button...
when i place images instead of button in gridview then onItemClickEvent get fired..but if i place button in gridView then click event not getting callled...i dont know what is the problem...even i m not getting exception..
here is my code...
public class MainMenu extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.mainMenu);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Toast.makeText(MainMenu.this, "hello" + position, Toast.LENGTH_SHORT).show();
}
});
}
//inner class for adapter
class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;
if (convertView == null) { // if it's not recycled, initialize some attributes
btn=new Button(mContext);
// imageView = new ImageView(mContext);
btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)
// imageView.setPadding(8,8, 8, 8);
} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}
// references to our images
private Integer[] mThumbIds =
{
R.drawable.pantrylocator_icon,
R.drawable.volunteeropportunity_icon,
R.drawable.volunteerlocator_icon,
R.drawable.volunteermanagement_icon,
R.drawable.donationform_icon,
R.drawable.donationviamsg_icon,
R.drawable.donationvideo_icon,
R.drawable.virtualfooddrive_icon,
R.drawable.newevent_icon,
R.drawable.pressrelease_icon,
R.drawable.volunteerphotos_icon,
R.drawable.aboutus_icon,
};
}
}
The button has its own OnClickListener:
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
Button btn;
if (convertView == null) { // if it's not recycled, initialize some attributes
btn=new Button(mContext);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
// imageView = new ImageView(mContext);
btn.setLayoutParams(new GridView.LayoutParams(120,120));
// imageView.setLayoutParams(new GridView.LayoutParams(140,140));
//imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
btn.setPadding(10,15, 10,15);
btn.setImeActionLabel("hello",0);// actionId)
// imageView.setPadding(8,8, 8, 8);
} else
{
btn=(Button)convertView;
//imageView=(ImageView)convertView;
}
btn.setBackgroundResource(mThumbIds[position]);
//imageView.setImageResource(mThumbIds[position]);
//return imageView;
return btn;
}
In your ImageAdapter-> getView method add the following line before returning newly created "convertView"
convertView.setClickable(false);
convertView.setFocusable(false);
If any of the views in gridview are clickable then they will block the grid's ItemClick listener from responding.
There is no onclick event written for the Buttons you are adding. Write code for the buttons to handle the click event! let us know then.
i have fece this problem also but finally got the solution i have follow the above suggession
and define button click event in base adapter class like as
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = LayoutInflater.from(mContext);
v = li.inflate(R.layout.icon, null);
tv = (Button)v.findViewById(R.id.icon_text);
iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(mThumbIds[position]);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(mContext, "vim", Toast.LENGTH_LONG).show();
}
});
}
else
{
v = (View)convertView;
}
return v;
}
gridview = (GridView) findViewById(R.id.gameGrid);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Ur Code here
}
Add click events for the button added in gridView
Here's the cleanest way to do it: call performItemClick() on the GridView from within each button's click listener. That way you can still use the GridView's onItemClickListener like normal.
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
...
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView) parent).performItemClick(v, position, 0);
}
});
}
http://www.migapro.com/click-events-listview-gridview/
I have solved my problem as i define button click event in base adpter class and my problem is solved......