I have a for loop which creates views dynamically.
for (int i=0;i<5;i++){
a=new ImageView();
tv=new TextView();
img=new ImageView();
spn=new Spinner();
img.setOnClickListener(this);//this is working for every view
spn.post(new Runnable){
#Override
public void run() {
Log.d("post","inside post");// here only last view is working
}
} //for loop ends
But how to create spinner.post dynamically or make it work.
Please help. Stuck on it for last three days.Thanks in advance.
Make your activity implementes OnItemSelectedListener, which is the callbak registered for every item selected in the spinner, and then inside this callback implements what you need on every view.
Your class definition:
public class SpinnerActivity extends Activity implements OnItemSelectedListener
Implements onItemSelected callback in your activity:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
If you need documentation look at this http://developer.android.com/guide/topics/ui/controls/spinner.html ;)
Related
I have a Main Activity that has a bunch of child views and then I have a ListView whose items are dynamically generated. I'd like to be able to long click any of these items and have this event handled in the Main Activity.
MainActivity
LinearLayout
ListView
Item - (long click here)
Now I know how to subscribe the the onLongClick event of the dynamically created ListView item itself but that's in the adapter I use to create the items.
What I was hoping was, that I could simply subscribe to the onLongClick event of the Main Activity and I'd see the event from the dynamically created ListView item propagate up to the MainActivity. But that's not happening.
My MainActivity implements View.OnLongClickListener, like so
class MainActivity : Activity(), View.OnLongClickListener {
override fun onLongClick(v: View?): Boolean {
Log.d("APP", "LongClicked: ")
return true
}
......
}
However, I'm not sure how to set the handler in the Activity itself. That is, for onClick, I would set android:onClcik="onCLick", in the XML of the Main Activity, however, I don't see an option to do the same for onLongClick
Also, I have not set any attributes such as android::longClickable="true/false"
any of the views. I'm a bit confused by the true/false and what the actually mean.
Any help would be appreciated.
UPDATE
I tried calling the setOnLongCLickListener on the ListView in the Main Activity, since I have access to the ListView there. Like so
availableZonesListView = findViewById(R.id.availableZonesListView)
availableZonesListView!!.onLongClickListener(this)
The MainActivity ('this') implements the View.OnLongClickListener and I see events coming in there when I long click directly on the MainActivity/Layout. But I don't see eventing coming in there when I long click on the items of the ListView
Now I know how to subscribe the the onLongClick event of the dynamically created ListView item itself but that's in the adapter I use to create the items.
public MainActivity extends .. implements MyClickListener {
... onCreate {
... = new MyAdapter(yourParams, this); // `this` is the new param
}
#Override
public void onAnyItemLongClicked() {
// Do whatever you want
}
}
Create your new interface:
interface MyClickListener {
void onAnyItemLongClicked();
}
Then in your adapter where you "Now I know how to subscribe the the onLongClick event". It would be something like this:
public class MyAdapter {
// other fields
private final MyClickListener listener;
public MyAdapter(otherParams, MyClickListener listener) {
.. assign other params
this.listener = listener;
}
}
// However you set your item click listener (in getView)
adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
listener.onAnyItemLongClicked();
}
});
This will propagate the click event from the adapter to the activity.
(I had to improvise a bit because you didn't share many code details)
I have a ListView with Custom Adapter. I have seen this thread where people asked if the items in the custom view had a clickable item. And Yes, I have a clickable ImageView in the listrow. So clicking anywhere else(other than that ImageView) should perform some other action. I gave an onItemClickListener to the ListView. However, it doesn't work on first click and works on two-three clicks.
Update:
In my adapter's getView method, I set onClick of ImageView like this:
holder.chatImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff here
}
});
It works fine, and in my activity, I gave onItemClickListener to listView likw this:
onlineListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//Stuff here
}
});
PS: I didn't explicitly give any focus to anything.
Some time ago I've faced the same problem but with a CheckBox, I've resolve it by creating a onClickListener as member of the Custom Adapter. For example
public MyAdapter extends BaseAdapter{
// ... ..
// ... ...
private OnClickListener imageClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
/// your data .getTag()
// process onClickListener for image
}
};
}
And then, in your getView:
if (convertView == null){
//Create your views and register onClickListener
holder.chatImageView.setOnClickListener(imageClickListener);
}
Add android:focusable="false" to your image in xml.
Hope it helps, Here you have the entire example:
I have a Layout in Android which has a ListView which then inflates another layout as its rows using an adapter, pretty standard.
The row layout which is inflated X times depending on how many rows there needs to be contains a spinner. I add a seSelectedItemListener to the Spinner yet when the Spinner is pressed, the options are displayed, then an item is pressed/selected, then the OnItemSelectedListener constructor is fired yet the overrided onItemSelected is not. If i inflate the row layout seprately not in a List View and select an item in the Spinner then the onSelectedItem fires...
Any ideas why this happens or how/if it can work this way?
OnItemSelected class:
class SelectedSizeChangeListener implements OnItemSelectedListener {
Spinner product_size;
TextView product_sell_price;
TextView product_cost_price;
Drink item;
Drink_Size drink_size;
SelectedSizeChangeListener()
{
String s = "here1";
}
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String s = "here2";
}
public void onNothingSelected(AdapterView parent) {
String s = "here3";
}
}
Adding the onItemSelected class to the spinner:
product_size_spinner.post(new Runnable() {
public void run() {
product_size_spinner.setOnItemSelectedListener(new SelectedSizeChangeListener());
}
});
Any help would be great!
Adrian
Try
android:focusable="false"
android:focusableInTouchMode="false"
in list view
Pro-tip:
try to use PopUpMenu rather than Spinner.
Example android-popupwindow in list View
I know that ListActivity gives me the possibility to use onListItemClick.
On the other hand in a normal Activity i can include multiple lists and make easy switching+animation through a ViewFlipper.
So. Can i make it work alltogether?
[Solved] Yes! ... implements are the kings. Deleted my code to minimize confusion.
Have you tried having your Activity implement OnItemClickListener and then set your lists' onItemClickListener to the activity? I.e.
public class MyActivity extends Activity implements OnItemClickListener {
...
public void onCreate(...) {
...
mList1.setOnItemClickListener(this);
mList2.setOnItemClickListener(this);
...
}
...
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
if (adapter.getId() == R.id.list1) {
// Handle list1 click event
} else if (adapter.getId() == R.id.list2) {
// Handle list2 click event
}
}
}
I am new to android and right now I am learning about ListView.
I was reading tutorials from bogotobogo.com when I saw this code:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
*** lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
}
}); ***
i was not able to understand code from lv.setOnItemClickListener(new OnItemClickListener(); is it an argument?
Can some one help me to understand it?
What that code is doing is implementing a new OnItemClickListener inline. The OnItemClickListener interface is basically a contract that says that an object will provide an implementation of the function onItemClick(....). Later on, when an item in your list gets clicked, the onItemClick function will be called and the AdapterView (thing that's instantiating and managing the list rows, the view - (the render code for a particular row), the position (the position in the list) and an id property which I never use so you can look up what that's for are being passed in.
Inline code like this always looks strange to me. There are a couple other ways to write this that I think make more intuitive sense. Just keep in mind that what you're doing is writing some code to get executed when a row in your list gets clicked.
1 - You can have your Activity implement OnItemClickListener
public class SomeActivity extends Activity implements OnItemClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourview);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//Your toast code goes in here
}
}
In this code you're having your main class implement the OnItemClickListener interface so setOnItemClickListener sees the main class (this) as an instance of OnItemClickListener. When a row in your list gets clicked the onItemClick function will get called.
You can also 2 - have your click listener come from an internal class.
public class SomeActivity extends Activity implements OnItemClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourview);
ListView lv = (ListView)findViewById(R.id.listView);
lv.setOnItemClickListener(new YourInternalClass());
}
class YourInternalClass implements View.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//Your toast code goes in here
}
}
}
And really all three methods are doing the same thing: Providing the setOnItemClickListener with an instance of a View.OnItemClickListener class that will have it's onItemClick function called when a row in the list gets clicked.