在android listview上添加一个按钮

时间:2015-04-19 19:38:57

标签: android android-listview android-button

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

main.xml仅包含ListView

自定义-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);


    }
});

代码正常工作,当我点击列表视图项时,它会打开进行共享,但是我需要它在单击按钮时打开,因为当前我试图链接按钮但我不能。任何帮助

2 个答案:

答案 0 :(得分:0)

这里是你可以做的,在你的按钮xml

后面加两行
android:focusable="false"
android:focusableInTouchMode="false"

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

答案 1 :(得分:0)

您不需要使用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"