0

I am trying to make a listView with two buttons on each row. I have an adapter where the buttons and the rest views of my list are declared. My problem is that I can not fire the buttons from my main activity. I thought the code below should work but it didn't.

  public class Zmenu extends Activity {

   final EfficientAdapter Myadapter=new EfficientAdapter(this);

    final ListView l1 = (ListView) findViewById(R.id.ListView01);

    l1.setAdapter(Myadapter);

    l1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
                    switch(arg1.getId()) {
            case R.id.Button1 :
                    //do this block
                break;
        case R.id.Button2 :
                   //do this block
                break;
        }
}});
}

Can anyone helps me on what I am doing wrong in how could I fire the key listener in my main activity?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
George32x
  • 1
  • 1
  • 2
  • try by adding the button's action listener in the getView() of the adapter. – Salil Apr 26 '11 at 12:09
  • Wait... But where is the `setContentView(R.layout.my_list);`? – Kocus Apr 26 '11 at 12:11
  • Ii havent invole the whole code here just what I think is important. It works adding the listener in the getView of the adapter but I wqas wondering how I could do this from my main activity. – George32x Apr 26 '11 at 12:46

2 Answers2

1

Either you will need to let the events from the button percolate to the parent listview (see How to pass the onClick event to its parent on Android?) or in your EfficientAdapter's getView() function, you need to register the listener with each button in the item.

Community
  • 1
  • 1
Rajath
  • 11,787
  • 7
  • 48
  • 62
0

EDIT: you don't have the correct code layout. You need to implement your code in an onCreate function

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//here goes your layout

        final EfficientAdapter Myadapter=new EfficientAdapter(this);

        final ListView l1 = (ListView) findViewById(R.id.ListView01);

        l1.setAdapter(Myadapter);

        l1.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                switch(arg1.getId()) {
                        case R.id.Button1 :
                            //do this block
                            break;
                        case R.id.Button2 :
                            //do this block
                            break;
                    }
                }
          });
 }
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • Yes I have already do this the point is if the "setOnItemClickListener(new OnItemClickListener()" should work or and how can I fire an on key listener from my main activity – George32x Apr 26 '11 at 13:00