I am trying to attach an OnClickListener on the child elements in a custom list view.The class that I am using extends ListActivity. In my custom list view I have two text views and a button. I need to attach an OnClickListener on each of them. The following is my code.
final ListView listview = getListView();
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
callBut= (Button)listview.getChildAt(position);
callBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Call Phone","");
}
});
}
But when I click on the child element Nothing is shown on the log.
You can atach a OnItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
change
callBut= (Button)listview.getChildAt(position);
to
callBut= (Button)findViewById(R.id.yourCallButton);
Related
I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}
I have been trying to solve this simple problem for two days now without success. I know a similar question have been asked before (like here:
How do you get the selected value of a Spinner? ), but I haven't been able to solve this yet.
So, I have created a simple app which contains a spinner and a custom ListView. The spinner (containing values 1,2,3,4,5..) creates more spinners (which contain Player names) to the ListView (eg. choosing 4 creates four spinners). Currently, the program works fine except that I don't know how to get the selected values of the spinners in ListView when clicking a button in Action Bar? I tried to use getItemAtPosition and getSelectedXXX() methods but I always tend to the get values from a single spinner I recently selected.
I am not sure do I have to invoke the methods in the Spinner's setOnItemSelectedListener(), or can I call the values from outside the SpinnerAdapter class.
Here is the custom Spinner class
public class PlayerAdapter extends ArrayAdapter<String> {
public PlayerAdapter(ArrayList<String> mPlayers) {
super(getActivity(), 0, mPlayers);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.list_item_players, null);
}
Spinner mSpinnerPlayer = (Spinner) convertView
.findViewById(R.id.spinner_player);
ArrayAdapter<Player> spinnerAdapterPlayer = new ArrayAdapter<Player>(getActivity(), android.R.layout.simple_spinner_item, mPlayers);
spinnerAdapterPlayer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerPlayer.setAdapter(spinnerAdapterPlayer);
mSpinnerPlayer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
//Should I use getItemAtPosition or getSelectedXXX methods here?
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
return convertView;
}
}
}
Here is the Fragment which inflates the layout creates the first spinner (sorry about the formatting and code style)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
setHasOptionsMenu(true);
//gets player names from a database, such as "Player 1", "Player 2" etc..
//this is container of the spinners displayed in the listview
mPlayers = PlayerDB
.get(getActivity())
.getPlayers();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_menu, parent, false);
listView = (ListView) view.findViewById(R.id.listView1);
mSpinnerNumOfPlayers = (Spinner) view.findViewById(R.id.spinner_numOfPlayers);
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter
.createFromResource(this.getActivity(),
R.array.numberOfPlayers_array,
android.R.layout.simple_spinner_item);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerNumOfPlayers.setAdapter(spinnerAdapter);
mSpinnerNumOfPlayers.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String position = parent.getItemAtPosition(pos).toString();
mArray = new ArrayList<String>();
posInt = Integer.parseInt(position);
for (int i = 0; i < posInt; i++) {
mArray.add("" + i);
}
PlayerAdapter adapter = new PlayerAdapter(mArray);
listView.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
}
);
return view;
}
// ActionBar
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_start:
//when "start" button is clicked the spinner values are saved into an ArrayList
This method will retuns you the selected ite from the spinner..
spinner.getSelectedItem().toString()
I have a listview which I want to edit from a dialog box. I've read from googling that I need to notifyDataSetChanged() on the listview, however when I try to do this I get an error:
The method notifyDataSetChanged(View) is undefined for the type ListView
My listview is set originally at the top of my code with just a:
ListView listView;
It's then set in a routine on the onload
public void loadItems(){
//Removed - just getting the data
int rowCount;
rowCount = mCategory.size();
listView = (ListView) findViewById(R.id.lvItems);
int[] colors = {0, 0xFFFF0000, 0};
listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView.setDividerHeight(1);
listView.setAdapter(new CustomAdapter());
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
loadPopup(mDescription.get(position).toString(), mCountList.get(position).toString(), mTemplatecode.get(position).toString());
}
});
//Removed - Tidying up
}
The dialog box that is loaded on the onlick has a simple interface, a minus button, a plus button, a text box and a go button. The number in the textbox is changed which does a db call. When the dialog box closes I want it to refresh the listview behind to reflect the new change. I.e. basically re-run the loadItems() routine again.
This is what happens when you click the go button on the dialog. I'm obviously putting the notifyDataSetChanged in the wrong place as it wont even run.
btnGo.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick(View v) {
addAsset(v, AssetDesc, txt.getText().toString(), templateCode);
listView.notifyDataSetChanged();
loadItems();
dialog.dismiss();
}
});
Custom adapter:
class CustomAdapter extends BaseAdapter
{
#Override
public int getCount() {
return mDescription.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LayoutInflater inf=getLayoutInflater();
View v=inf.inflate(R.layout.noncriticalasset, arg2,false);
TextView tv=(TextView)v.findViewById(R.id.txtOption);
TextView tvCount=(TextView)v.findViewById(R.id.txtCount);
tv.setText(mDescription.get(arg0).toString());
tvCount.setText(mCountList.get(arg0).toString());
return v;
}
}
You need to call notifyDataSetChanged on the adapter, not the ListView itself.
I am using spinner that shows error when i am trying to extract the item id of the selected spinner item.
My Code goes here:
public void dispspi()
{
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter <String> adap= new ArrayAdapter(this, android.R.layout.simple_spinner_item, level);
spinner.setAdapter(adap);
spinner.setOnItemClickListener(new OnItemClickListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int item = spinner.getSelectedItemPosition();
p=item;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
How to get the item id of the spinner? Any help is appreciated..Thanks in advance
IIRC, you should be using a selected listener, not click:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
Then you can add the override tag to your selected method.
private String selecteditem;
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
selecteditem = adapter.getItemAtPosition(i).toString();
//or this can be also right: selecteditem = level[i];
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int postion, long arg3) {
// TODO Auto-generated method stub
String SpinerValue3 = parent.getItemAtPosition(postion).toString();
Toast.makeText(getBaseContext(),
"You have selected 222 : " + SpinerValue3,
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Yes you can use some OnItemSelectedListener for work with selected item. But sometimes we would like to handle exactly click for spinner. For example hide keyboard or send some analytics etc. In this case we should use TouchListener because OnClickListener doesn't work properly with Spinner and you can get error. So I suggest to use TouchListener like:
someSpinner.setOnTouchListener { _, event -> onTouchSomeSpinner(event)}
fun onTouchSomeSpinner(event: MotionEvent): Boolean {
if(event.action == MotionEvent.ACTION_UP) {
view.hideKeyBoard()
...
}
return false
}
you should have this in the listener(OnItemSelectedListener)
public void onNothingSelected(AdapterView<?> arg0) {
}
It might works without it but put it to be consistent
but there might be other errors also, can you provide the error log ?
i have this code but i can't see the action when i click on a item from the list.
This code shows me the info on ArrayList profilesArrayList but i dont know how i check what item i am selecting form the listview. Anyone can help me?
profilesArrayList = new ArrayList<Profile>();
profilesArrayList = copyProfilesToArrayList();
ProfileAdapter adapter = new ProfileAdapter(
getApplicationContext(), R.layout.profiles_item, profilesArrayList);
listViewProfiles = (ListView)findViewById(android.R.id.list);
listViewProfiles.setAdapter(adapter);
listViewProfiles.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
switch(position) {
case 0:
Log.d("cardNumber", profilesArrayList.get(0).getCardNumber());
break;
If your container class for this code is the ListActivity, just override the onListItemClick for that class, rather than set it as an OnItemClickListener for the view. That works for me
public class ProfileList extends ListActivity
{
private ArrayList<Profile> profilesArrayList;
#Override
public void onCreate ( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
//populate your arraylist
setListAdapter ( new ArrayAdapter<Profile>() );
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Log.d("cardNumber", profilesArrayList.get(position).getCardNumber());
}
}
Try to use getSelectedItemPosition() and add INVALID_POSITION as one of your possible cases