0

I have created a custom listview using SimpleAdapter and in each row of list view i put a button has single id. I want to get the position of each row to pass the button but i have a single button id for each row and I want when I click on button it find the position of row and start another activity please help me

public void click(View v){
    //RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
    TextView tv = (TextView)findViewById(R.id.text1);
    ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
     ListView lv=(ListView)findViewById(android.R.id.list);
    int position = 0;
    Long id=Long.parseLong((String) adapter.getItem(position));

    Intent i=null;
    switch(position){
    case 1:
      i=new Intent(this, ButtonActivity.class);
        startActivity(i);
        break;
    case 2:
         i = new Intent(this, PickerActivity.class);
        startActivity(i);
        break;
    }
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Naveen Kumar
  • 3,738
  • 4
  • 29
  • 50

3 Answers3

0

On list view, you can use setOnItemClickListener

list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {  
   public void onItemSelected(AdapterView parentView, View childView, int position, long id) {  
            //Put Your code here 
        }
        public void onNothingSelected(AdapterView parentView) {  

        }  
      });  
RPB
  • 16,006
  • 16
  • 55
  • 79
0

I am not sure about this, but lets try it, and inform me about your success..

The line , int position = listView.getPositionForView(v.getParent());

public void click(View v){

    // This line is important
    int position =  listView.getPositionForView(v.getParent());

    //RelativeLayout navi = (RelativeLayout)findViewById(R.layout.custom_row_view);
    TextView tv = (TextView)findViewById(R.id.text1);
    ImageButton im = (ImageButton)findViewById(R.id.imageButton1);
     ListView lv=(ListView)findViewById(android.R.id.list);
    int position = 0;
    Long id=Long.parseLong((String) adapter.getItem(position));

    Intent i=null;
    switch(position){
    case 1:
      i=new Intent(this, ButtonActivity.class);
        startActivity(i);
        break;
    case 2:
         i = new Intent(this, PickerActivity.class);
        startActivity(i);
        break;
    }
  }

If this not works then only option you have to is making a Custom Adapter and write your button's onClick() in adapter's getView() in which you have a position variable accessible.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • This won't work, instead see [this](http://stackoverflow.com/questions/11241455/listview-item-with-button-and-know-what-item-the-button-was-inside-after-click/11241552#11241552) answer. – Lalit Poptani Jun 28 '12 at 11:05
  • @LalitPoptani - I don't know this works or not, But OP has not write any getView() to set a Tag for buttons. – user370305 Jun 28 '12 at 11:07
  • Also as per document concern If every List row has a button which on inflated at runtime then this will definitely works. – user370305 Jun 28 '12 at 11:09
  • I dont say you are wrong or this wont work, cause i have still to try this your way, but @user370305 I think he has mentioned in his Question that he has a Custom Listview and I think all custom listview has one custom adapter class with getView() method. – MKJParekh Jun 28 '12 at 11:09
  • Yeh you are right I should have gone through all the comments then I would have found the last comments "for list creatation i used simple adapter", nice that you have upvoted the comment now i can easily see that. – MKJParekh Jun 28 '12 at 11:14
  • yes i made a custom list in which a button on each row and i want to start activities by each row button – Naveen Kumar Jun 28 '12 at 11:14
0

try this

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    final int arg2, long arg3) {

                final ProgressDialog progressBar = ProgressDialog.show(
                        VechiclesDetails.this, "", loadingString);

                progressBar.setIndeterminate(true);
                progressBar.setIndeterminateDrawable(getResources()
                        .getDrawable(R.anim.progressbar_handler));
                new Thread(new Runnable() {

                    public void run() {
                        try {
                            Intent carDetail = new Intent(
                                    Details.this,
                                    Screen.class);
                            carDetail.putExtra("index", arg2);
                            carDetail.putExtra("sellerId", SellerId);
                            startActivity(carDetail);
                            progressBar.dismiss();
                        } catch (Exception e) {
                            progressBar.dismiss();
                        }
                    }
                }).start();

            }
        });