2017-02-24 5 views
0

Tout d'abord je sais qu'il y a des centaines de ces questions mais les réponses n'ont pas résolu mon problème. Je pense que c'est quelque chose de petit qui me manque, fondamentalement j'ai ajouté un RecyclerView et ai besoin de commencer une nouvelle activité quand une carte est cliquée.RecyclerView OnClicklistener ne répond pas

J'ai suivi ce tutoriel et ça a été fluide jusqu'à ce point. https://guides.codepath.com/android/using-the-recyclerview

C'est le recycleur

<FrameLayout 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" 
    tools:context=".MainActivity"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/bgImg" 
     android:scaleType="centerCrop"/> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/RV" 
     /> 

</FrameLayout> 

Ceci est la carte

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="4dp" 
    android:layout_margin="5dp" 
    android:clickable="true" 
    android:background="?android:attr/selectableItemBackground"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:id="@+id/vendor_image2" 
     android:layout_width="fill_parent" 
     android:layout_height="240dp" 
     android:padding="5dp" /> 

    <RelativeLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/vendor_image2" 
     > 

     <TextView 
      android:id="@+id/vendor_name2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="bold" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:paddingBottom="5dp"/> 

     <TextView 
      android:id="@+id/vendor_content2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="15dp" 
      android:layout_marginTop="7dp" 
      android:layout_toEndOf="@+id/vendor_name2" 
      android:textColor="@color/text_col" 
      android:paddingBottom="5dp" /> 

     <TextView 
      android:id="@+id/vendor_suburb2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="italic" 
      android:textColor="@color/text_col" 
      android:layout_below="@+id/vendor_name2" 
      android:layout_marginLeft="10dp" /> 
     <RatingBar 
      android:id="@+id/MyRating2" 
      style="?android:attr/ratingBarStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/vendor_suburb2" 
      android:layout_marginLeft="10dp" 
      android:isIndicator="true" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:numStars="5" 
      android:textColor="@android:color/black" 
      android:rating="3.5" 
      android:stepSize="0.1" /> 

    </RelativeLayout> 
</LinearLayout> 
    </android.support.v7.widget.CardView> 

Et enfin l'adaptateur et gestionnaire

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RatingBar; 
import android.widget.TextView; 

import com.iconiccode.android.guestloc8tor.SQL.DatabaseHandler; 
import com.nostra13.universalimageloader.core.DisplayImageOptions; 
import com.nostra13.universalimageloader.core.ImageLoader; 
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 
import com.nostra13.universalimageloader.core.assist.ImageScaleType; 
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.List; 

public class VendorRecycleAdapter extends RecyclerView.Adapter<VendorRecycleAdapter.VendorHolder> 
{ 
    public List<Vendor> vendorList; 
    private DatabaseHandler dbHandler = new DatabaseHandler(MyApp.getContext()); 
    private Vendor currentVendor; 
    int cate; 
    Activity thiscontext; 
    public VendorRecycleAdapter(Activity context, int Cate) 
    { 
     this.cate=Cate; 
     this.thiscontext=context; 
    } 

    @Override 
    public VendorHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    { 
     LayoutInflater inflater = LayoutInflater.from(thiscontext); 

     //View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false); 
     View view = inflater.inflate(R.layout.vendor_list_cards,parent,false); 
     return new VendorHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(VendorHolder holder, int position) 
    { 
     vendorList = dbHandler.getVendorData(cate); 
     currentVendor = vendorList.get(position); 

     // Get the image string from the Vendor object 
     String vendorImage = currentVendor.getImage(); 

     Log.e("Vendor Adapter Image", vendorImage); 

     // Get the name string from the Vendor object 
     String vendorName = currentVendor.getCompanyName(); 

     Log.e("Vendor Adapter Name", vendorName); 

     // Get the content string from the Vendor object 
     String vendorContent = currentVendor.getContent(); 

     // Get the location string from the Vendor object 
     String vendorSuburb = currentVendor.getSuburb(); 

     Log.e("Vendor Adapter Suburb", vendorSuburb); 

     // Find the TextView with view ID vendor_name 

     // Display the name of the current vendor in that TextView 
     holder.Vvendor_name.setText(vendorName); 

     holder.Vvendor_image.setImageResource(R.drawable.loading); 

     ImageLoader imageLoader; 
     DisplayImageOptions options; 
     imageLoader = ImageLoader.getInstance(); 

     String url = vendorImage; 
     imageLoader.init(ImageLoaderConfiguration.createDefault(MyApp.getContext())); 
     options = new DisplayImageOptions.Builder() 
       .showImageForEmptyUri(R.drawable.loading) 
       .showImageOnFail(R.drawable.b4f29385) 
       .resetViewBeforeLoading(true).cacheOnDisk(true) 
       .imageScaleType(ImageScaleType.EXACTLY) 
       .bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true) 
       .displayer(new FadeInBitmapDisplayer(300)).build(); 

     imageLoader.displayImage(url, holder.Vvendor_image); 

     // Find the TextView with view ID vendor_content 
     holder.Vvendor_content.setText(vendorContent); 

     holder.Vvendor_suburb.setText(vendorSuburb); 
    } 

    @Override 
    public int getItemCount() 
    { 
     vendorList = dbHandler.getVendorData(cate); 
     return vendorList.size(); 
    } 


    public class VendorHolder extends RecyclerView.ViewHolder implements View.OnClickListener 
    { 
     public ImageView Vvendor_image; 
     public TextView Vvendor_name,Vvendor_content,Vvendor_suburb; 
     public RatingBar Vrating; 
     public VendorHolder(View itemView) 
     { 
      //These pull as null 
      super(itemView); 
      Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2); 
      Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2); 
      Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2); 
      Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2); 
      Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2); 
     } 

     @Override 
     public void onClick(View v) 
     { 
//   int position = getAdapterPosition(); 
//   currentVendor = vendorList.get(position); 
      Intent intent = new Intent(thiscontext,VendorDetailsActivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
      String Category = Integer.toString(cate); 
      JSONObject message = currentVendor.getAllData(); 
      try { 
       intent.putExtra("IMAGE", message.getString("IMAGE")); 
       intent.putExtra("URL", message.getString("URL")); 
       intent.putExtra("CONTENT", message.getString("CONTENT")); 
       intent.putExtra("COMPANY_NAME", message.getString("COMPANY_NAME")); 
       intent.putExtra("TEL", message.getString("TEL")); 
       intent.putExtra("BOOKING_URL", message.getString("BOOKING_URL")); 
       intent.putExtra("LAT", message.getString("LAT")); 
       intent.putExtra("LON", message.getString("LON")); 
       intent.putExtra("STREET", message.getString("STREET")); 
       intent.putExtra("SUBURB", message.getString("SUBURB")); 
       intent.putExtra("PROVINCE", message.getString("PROVINCE")); 
       intent.putExtra("CITY", message.getString("CITY")); 
       intent.putExtra("CAT",Category); 
      } catch (JSONException json) 
      { 
       Log.e("ERROR", json.getMessage()); 
      } 
      thiscontext.startActivity(intent); 
     } 
    } 

} 

Toute aide serait grandement appréciée. Merci

Répondre

2

Je pense que vous avez juste oublié de régler la onClickListener dans votre constructeur ViewHolder:

public VendorHolder(View itemView) 
    { 
     //These pull as null 
     super(itemView); 
     Vvendor_image = (ImageView)itemView.findViewById(R.id.vendor_image2); 
     Vvendor_name = (TextView)itemView.findViewById(R.id.vendor_name2); 
     Vvendor_content =(TextView)itemView.findViewById(R.id.vendor_content2); 
     Vvendor_suburb = (TextView)itemView.findViewById(R.id.vendor_suburb2); 
     Vrating = (RatingBar) itemView.findViewById(R.id.MyRating2); 

     // If it should be triggered only when clicking on a specific view, replace itemView with the view you want. 
     itemView.setOnClickListener(this); 
    } 
+0

Je savais qu'il était quelque chose d'idiot, Pourriez-vous me montrer où exactement le placer? – Demonic217

+0

J'ai modifié ma réponse @ Demonic217 – mVck