【问题标题】:adding a button on android listview在android listview上添加一个按钮
【发布时间】:2015-06-26 09:52:42
【问题描述】:

我已从本地数据库中获取数据并将其显示在 ListView 中,并且我有一个带有按钮的自定义行

main.xml 将仅包含 ListView

custom-row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
style="@color/background_gradient_start"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.6"
android:background="@color/background_gradient_start"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="177dp"
        android:layout_height="75dp"
        android:layout_weight="5" >

        <TextView
            android:id="@+id/textViewFlightNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textViewCodes"
            android:layout_alignBottom="@+id/textViewCodes"
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/textViewCodes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="18dp"
            android:background="@color/transparent"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#000000"
            android:textSize="16dp" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="142dp"
        android:layout_height="match_parent"
        android:layout_weight="5">

        <Button
            android:id="@+id/btn_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:alpha="1"
            android:background="#3b3974"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:minWidth="90dp"
            android:text="Share"
            android:textColor="#f1ecef" />

    </RelativeLayout>

</LinearLayout>

活动

所以在创建下

String [] fromFiledName =new String[]{db.KEY_CODE};

int[] toViewIDs=new int[]{R.id.textViewCodes};

SimpleCursorAdapter myCurAdapter=new SimpleCursorAdapter(
        this, //context
        R.layout.listview_row_codes,
        cursor,
        fromFiledName,
        toViewIDs
        );


// set addapter to list view
ListView mylistPRN=(ListView) findViewById(R.id.codeslist);
mylistPRN.setAdapter(myCurAdapter);

mylistPRN.setAdapter(myCurAdapter);

mylistPRN.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //Intent intent2 = new Intent(this, PromoCodeActivity2.class);
        Intent sendIntent = new Intent();
       //intent.putExtra(PromoCodeActivity.requestString, httpRequestString);
        //getItemAtPosition(position);
        Cursor c = (Cursor) parent.getAdapter().getItem(position);

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, "share code "+c.getString(c.getColumnIndex("code")));
        startActivity(Intent.createChooser(share, "Share Text"));
        //startActivity(intentD);


    }
});

代码正在运行,当我单击列表视图项目时,它会打开以供共享,但我需要在单击按钮时打开它,因为目前我试图链接按钮但我做不到。任何帮助

【问题讨论】:

    标签: android android-listview android-button


    【解决方案1】:

    您可以在按钮 xml 中添加以下两行

    android:focusable="false"
    android:focusableInTouchMode="false"
    

    然后在适配器 getView 方法中将 onclickListener 附加到此按钮,您就完成了。目前没有监听器附加到行中的按钮。

    【讨论】:

      【解决方案2】:

      您不需要使用onItemClickListner,因为您没有单击列表项,而是必须在 getview 方法中的按钮上设置onClickListner

      类似这样的

       @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          LayoutInflater inflater = context.getLayoutInflater();
          View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
          Button yourBtn = (Button)rowView.findViewById(R.id.your_btn_id);
      
         yourBtn.setOnClickListener(new View.OnClickListener() {
      
              @Override
              public void onClick(View arg0) {
                  //Do your work here
              }
          });
          return rowView;
      
      }
      

      还可以在您的按钮上设置以下属性

      android:focusable="false"
      android:focusableInTouchMode="false"
      

      【讨论】:

      • 每次我把这个方法放在 onCreate 之后它都会显示错误,我必须删除 @override 并且它看不到上下文
      • 你不应该把它放在 onCreate 之后。您必须创建一个扩展 BaseAdapter 的自定义类,更多详细信息请访问 mkyong.com/android/android-listview-example 查看 2. 自定义 ArrayAdapter 示例
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      相关资源
      最近更新 更多