2017-02-24 2 views
0

Erstens weiß ich, dass es Hunderte von diesen Fragen gibt, aber die Antworten haben mein Problem nicht gelöst. Ich vermute, dass es etwas Kleines ist, das ich vermisse, im Grunde habe ich einen RecyclerView hinzugefügt und muss eine neue Aktivität starten, wenn eine Karte angeklickt wird.RecyclerView OnClicklistener reagiert nicht

Ich habe dieses Tutorial verfolgt und es war glatt bis zu diesem Punkt Segeln. https://guides.codepath.com/android/using-the-recyclerview

Dies ist der Recycler

<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> 

Dies ist die Karte

<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> 

Und schließlich der Adapter und Handler

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); 
     } 
    } 

} 

Jede Hilfe wäre sehr geschätzt. Dank

Antwort

2

Ich glaube, Sie einfach den OnClickListener in Ihrem ViewHolder Konstruktor gesetzt vergessen:

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

Ich wusste, es war etwas Dummes, würden Sie mir etwas ausmachen, zeigt, wo genau sie zu platzieren? – Demonic217

+0

Ich habe meine Antwort @ Demonic217 bearbeitet – mVck

Verwandte Themen