0

listviewを使用SimpleAdapterしてカスタムを作成し、リスト ビューの各行に単一の 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 に答える