0

我已经创建了一个自定义listviewSimpleAdapter并且在列表视图的每一行中,我都放置了一个具有单个 ID 的按钮。我想让position每一行的按钮传递按钮,但我每行都有一个按钮 id,我希望当我点击按钮时,它会找到行的位置并开始另一个活动, 请帮助我

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

3 回答 3

0

在列表视图中,您可以使用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) {  

        }  
      });  
于 2012-06-28T10:55:54.007 回答
0

我不确定这一点,但让我们尝试一下,并告诉我你的成功..

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

如果这不起作用,那么您必须做的唯一选择是制作一个自定义适配器并将您的按钮写入您可以访问位置变量onClick()的适配器getView()中。

于 2012-06-28T10:58:37.497 回答
0

试试这个

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

            }
        });
于 2012-10-22T12:28:06.840 回答