Reputation: 311
My activity has a ListView that has a custom ArrayAdapter. On my ArrayAdapter i have an image, a bunch of textboxes and a button.
On the getView of the adapter i get my button and set setOnClickListener. From the click listener i can get the index of the clicked item.
Now my problem is that i want to propagate that information to my main activity, where i want to handle my button click. I can save the index information in a static var, but i still don't know how to fire an event in my activity.
How do i do that?
I'm 6 days new to Android so, thanks iggy
Code:
My Activity:
public class MyClass extends Activity{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject);
myListView .setAdapter(myList);
...
}
}
Now in my Adapter
public class MyCustomArrayAdapter extends ArrayAdapter<myAnotherClass> {
...
....
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
doStuff;
}
});
}
}
I need to somehow fire a buttonclick from my main activity, but without loosing the possibility to read the index clicked in the list view.
Upvotes: 2
Views: 4237
Reputation: 10095
Here's an alternative and, what I think to be, more elegant solution.
Firstly, in your MyCustomArrayAdapter
class, define an interface:
public interface MyCustomRowButtonListener{
void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view);
}
Create an member variable of MyCustomRowButtonListener
in your ArrayAdapter
public class MyCustomArrayAdapter{
private MyCustomRowButtonListener mRowButtonListener;
//....
}
and add a parameter for the listener in the constructor
public MyCustomArrayAdapter(Context context, MyCustomRowButtonListener listener){
mContext = context;
mRowButtonListener = listener;
}
in the getView method:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mRowButtonListener.onCustomRowButtonClick(getItem(position),position,b);
}
});
}
and in your activity:
myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
Now let your activity implement MyCustomRowButtonListener
public class MyClass extends Activity implements MyCustomRowButtonListener{
...
public void onCreate(Bundle savedInstanceState) {
...
myListView = (ListView)findViewById(R.id.lvxml);
myList = new MyCustomArrayAdapter(this, myAnotherClassObject,this);
myListView .setAdapter(myList);
...
}
}
public void onCustomRowButtonClick(MyAnotherClass selectedItem, int position, View view){
Toast.makeText(this,"You have selected "+selectedItem,Toast.LENGTH_SHORT).show();
}
Upvotes: 3
Reputation: 4176
The index ist the position
variable.
Ist has to be final to use it here
public View getView(final int position, View convertView, ViewGroup parent)
You can inform your Activity
with a listener-interface.
Upvotes: 0
Reputation: 311
So I've solved it somehow. It's probably not the way to do it but it works.
I save my activity in a static var.
Button b = (Button)convertView.findViewById(R.id.myButtonInListView);
b.setTag(position);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int i = (Integer) v.getTag();
myStaticVarRep.myActivity.myMethod(i);
}
});
Upvotes: 0
Reputation: 8158
I'm going to assume your code looks a little like this.
public class <Your Class> ... implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
button.setOnClickListener(this);
}
}
To handle button click events, you need to have an onClick method that looks like this.
public void onClick(View v){
<Handle your button click in here>
}
Whenever you click your button, it'll invoke the onClick method. That is where you would manipulate your static var, fire off other methods, etc. I hope that helps.
Upvotes: -1