【问题标题】:How can I find my button ID's in a ListView row Android Java? [closed]如何在 ListView 行 Android Java 中找到我的按钮 ID? [关闭]
【发布时间】:2014-04-11 16:22:00
【问题描述】:

我有 1 个列表视图,5 行,所以当我运行代码时,我得到了 5 个 item_Titel、5 个 item_Datum、5 个 item_Intro 和 5 个 item_Button。但我不知道如何找到 R.Id。从每个项目,每一行。当您使用松散的文本视图和按钮时,您可以在 XML 中调用它,例如:@id/"clicked_button1"、@id/"clicked_button2'、@id/"clicked_button3" 等。

然后我可以使用开关盒并填充它, 示例:

public class OnClick implements OnClickListener {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
                   case R.id.clicked_Button1:
                 Url = 1;
                      break;

               case R.id.clicked_Button2:           
                         Url = 2;
                          break;

               case R.id.clicked_Button2:           
                         Url = 3;
                          break;

但是现在我使用的是列表视图.. 但是我怎样才能找出哪个 R.id."item_Button?!!!" 我现在需要用吗?

我希望我的问题/解释现在更清楚一点! ;-)

谢谢你帮助我!

添加了 XML item_view:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"
        android:layout_height="match_parent" >

            <TextView
                android:id="@+id/item_Titel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:textColor="#FFFFFF"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/item_Datum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_Titel"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:textColor="#FFFFFF"
                android:textSize="13dp" />

            <TextView
                android:id="@+id/item_Intro"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_Datum"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:textColor="#FFFFFF"
                android:textSize="15dp" />

            <Button
                android:id="@+id/item_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/item_Intro"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:layout_marginBottom="10dp"
                android:layout_marginRight="3dp"
                android:background="@drawable/mybutton"
                android:padding="9dp"
                android:textColor="#FFFFFF"
                android:textSize="10dp" />

     </RelativeLayout>

添加了 XML activity_main:

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:background="@drawable/tmx_background_portrait" 

        android:layout_width="match_parent"
        android:layout_height="match_parent"

        tools:context=".MainActivity" >

      <TextView
          android:id="@+id/TVLoading"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="3dp"
          android:layout_marginRight="3dp"
          android:layout_marginBottom="5dp"
          android:textSize="20dp"
          android:textColor="#FFFFFF" />

        <ListView
            android:id="@+id/PostListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" >

        </ListView>
    </RelativeLayout>

这是我的 MainActivity 的一部分,我尝试读取和控制我的按钮:

    public class OnClick implements OnClickListener {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {

            case R.id.item_Button???!!!:
                Url = 1;
                break;

    /*      
            case R.id.item_Button???!!!:
                Url = 2;
                break;

            case R.id.item_Button???!!!:
                Url = 3;
                break;
           */

               }
            startActivity(new Intent(MainActivity.this, SecondActivity.class));
        }
    }   

这里是 ListAdapter 部分和 getView 部分:

    private class MyListAdapter extends ArrayAdapter<Post> {
    public MyListAdapter() {
        super(MainActivity.this, R.layout.item_view, myPost);   
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, 
                             parent, false);
        }

        Post currentPost = myPost.get(position);

        // Titel:
        TextView TitelText = (TextView) 
                     itemView.findViewById(R.id.item_Titel);
        TitelText.setText(Html.fromHtml(currentPost.getTitel()));

        // Datum:
        TextView DatumText = (TextView) 
                     itemView.findViewById(R.id.item_Datum);
        DatumText.setText(Html.fromHtml(currentPost.getDatum()));

        // Intro:
        TextView IntroText = (TextView) 
                     itemView.findViewById(R.id.item_Intro);
        IntroText.setText(Html.fromHtml(currentPost.getIntro()));

        // Button:
        Button ButtonNumber = (Button) 
                     itemView.findViewById(R.id.item_Button);
        ButtonNumber.setText(Html.fromHtml(currentPost.getButton()));
        ButtonNumber.setOnClickListener(new OnClick());

        return itemView;

    }               
}

【问题讨论】:

  • 为什么ID是随机的?它们没有在您的 XML 布局中定义吗?如果它们是在您的 java 中创建的,则在创建它们时保留对它们的引用。如果您的问题是关于查找名为 OnClick 的按钮(您已将相同的 OnClickListener 分配给许多按钮),那么您可以使用作为参数传入的“视图”并将其转换回按钮。我不太确定这个问题在问什么,但这可能会有所帮助。
  • 你有 5 个 Listview 项目,每个项目都包含一个 Button,还是每个 ListView 项目都包含 5 个 Button?
  • 把你的XML和一些代码贴出来就很容易理解了。
  • 你需要有一个自定义的适配器类,你可以在你的按钮中设置onClickListener。看看this例子
  • 您应该创建一个项目类和一个自定义视图充气机。看这里:link

标签: java android xml listview button


【解决方案1】:

据我了解,您有一个填充了 5 行的列表视图。在每一行中,您都有文本和按钮。

当你在数组适配器中为你的行充气时,你需要获取按钮并附加监听器。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(mLayoutResourceId, parent, false);
    }
    Button button = (Button)row.findViewById(R.id.item_Button);
    button.setOnClickListener(this);
}

这是与您的问题类似的链接。 click listener for button inside list view in android

【讨论】:

    【解决方案2】:

    您可以使用 getView() 函数中的“int position”变量来推断它是列表中的哪个项目。然后,基于此分配 URL,而不是基于您的按钮 ID(您可以确定“位置”变量将对应于适配器中视图的顺序)。根本不要使用您的 OnClick 类。在确定要在 getView() 函数中使用的 URL 之后,您可以使用 setTag() 和 getTag() 函数在实际的 Button 实例(或任何扩展 View 的对象实例)。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.item_view, 
                             parent, false);
        }
    
        ...
    
        String url;
        switch(position){
            case 0: url = "url for the 1 button"; break;
            case 1: url = "url for the 2 button"; break;
            case 2: url = "url for the 3 button"; break;
            case 3: url = "url for the 4 button"; break;
            case 4: url = "url for the 5 button"; break;
            default: url = "http://www.google.com"; break;
        }
        Button button = (Button)itemView.findViewById(R.id.item_Button);
        button.setTag(url);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = (String)v.getTag();
                //do what you need with URL
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
        return itemView;
    }
    

    【讨论】:

      【解决方案3】:
              // Button:
              Button ButtonNumber = (Button) itemView.findViewById(R.id.item_Button);
              ButtonNumber.setTag(currentPost.getButtonid()); 
              ButtonNumber.setText(Html.fromHtml(currentPost.getButton()));
              ButtonNumber.setOnClickListener(new OnClick());
      
              Log.e("ButtonNumber.setId", "Found:" + ButtonNumber.getTag());
      

      我在其中添加了带有 .setTag() 的行。现在效果很好!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        • 2023-04-11
        相关资源
        最近更新 更多