how to pass the value of row in listview to a button

て烟熏妆下的殇ゞ 提交于 2019-12-20 07:37:50

问题


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;
    }

回答1:


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();

            }
        });



回答2:


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.




回答3:


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) {  

        }  
      });  


来源:https://stackoverflow.com/questions/11243040/how-to-pass-the-value-of-row-in-listview-to-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!