【问题标题】:how to pass the value of row in listview to a button如何将列表视图中的行值传递给按钮
【发布时间】:2012-06-28 10:52:13
【问题描述】:

我使用SimpleAdapter 创建了一个自定义listview,并且在列表视图的每一行中我放置了一个按钮 具有单个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;
    }

【问题讨论】:

  • 你想在点击按钮还是点击行时开始活动?
  • 如果你让你列出适配器类,那么在 getView 方法中你可以为特定行的按钮设置 onClickListner
  • 我为每一行设置了按钮,我想通过点击每一行上的按钮来开始一个活动。
  • 自定义适配器将完成您的工作,如上所述
  • 为了创建列表我使用了简单的适配器

标签: android android-listview


【解决方案1】:

试试这个

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】:

    对此我不确定,但让我们尝试一下,告诉我你的成功..

    线路,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(),其中您有一个位置变量 可访问。

    【讨论】:

    • 这行不通,请参阅this 答案。
    • @LalitPoptani - 我不知道这是否有效,但 OP 没有编写任何 getView() 来为按钮设置标签。
    • 也根据文档问题如果每个列表行都有一个在运行时膨胀的按钮,那么这肯定会起作用。
    • 我不是说你错了或者这行不通,因为我仍然要按照你的方式尝试,但是@user370305 我认为他在他的问题中提到他有一个自定义列表视图,我认为所有自定义列表视图有一个带有 getView() 方法的自定义适配器类。
    • 是的,你是对的,我应该浏览所有的 cmets,然后我会找到最后一个 cmets“用于创建列表,我使用了简单的适配器”,很高兴你对评论进行了投票,现在我可以很容易地看到那个。
    【解决方案3】:

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

    【讨论】:

    • 我想开始新的活动点击按钮我可以使用上面的代码
    • 为什么你只希望它出现在按钮上,如果你点击/点击按钮或整行,这将打开你的活动。 :)
    • 我要做的是使用按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多