Android:如何设置CardView OnLongClick()?

时间:2017-05-24 03:00:33

标签: android nullpointerexception android-cardview onlongclicklistener

我有一个CardViews的RecyclerView列表。我点击一个CardView,它使用一个意图来启动一个新活动,向用户显示一个详细的CardView。我想在新的CardView上添加一个LongClick,它将启动一个Dialog来询问用户是否要删除CardView。我尝试了下面的设置。对话框没有启动,我也没有看到Toast。我在这里缺少什么?

public class CardViewDetails extends AppCompatActivity {

    int position;
    CardView cardView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

    CardView cardView = (CardView) findViewById(detsinglecard_view);
    cardView.setOnClickListener((View.OnClickListener) this);
    cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Toast.makeText(CardViewDetails.this,"Dialog Test",Toast.LENGTH_SHORT).show();                
            android.app.FragmentManager fm = getFragmentManager();
            DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
            delCardViewDialog.show(fm,"delcardview dialog");
            return true;
        }
    }); 

activity_details:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.wimso.v060B.CardViewDetails">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<RelativeLayout
    android:id="@+id/cardViewDetails"
    android:layout_width="match_parent"
    android:layout_height="match_parent"        
    android:background="@color/background4main"
    android:layout_marginTop="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginLeft="6dp"
    android:layout_marginEnd="6dp"
    android:layout_marginRight="6dp"  >

<TextView        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="2dp"        
    android:textStyle="bold"
    android:textColor="@color/colorFlLabelFinal"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:clickable="false"
    android:layout_centerHorizontal="true"  />

<Spinner
    android:id="@+id/skycard_filter2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    style="@style/Base.Widget.AppCompat.Spinner"  />

**strong text**<android.support.v7.widget.CardView**strong text**

xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
**strong text**android:id="@+id/detsinglecard_view"**strong text**
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="4dp"
android:longClickable="true"  >    
...

1 个答案:

答案 0 :(得分:2)

在通过var science = new Class(); science.Name = "Sci-101"; science.Students.Add(new Student { Name = "Tom" }); science.Students.Add(new Student { Name = "Jerry" }); using (var dbCtx = new SchoolDBEntities()) { // Add the class dbCtx.Classes.Add(science); // Save whole entity graph to the database. // You do not need to save the students separately since they are part // of the class. We have already added them to the class so saving the // class will save the students added to the class as well. dbCtx.SaveChanges(); }

访问元素之前,您必须在setContentView()中致电onCreate()
findViewById()