2017-10-09 6 views
1

Je crée une liste de cartes et je voudrais obtenir le texte à l'intérieur de la TextView que la carte cliquée a.Obtenir le TextView à l'intérieur d'une carte

Dans mon adaptateur ai ceci:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 

    ImageView productImage; 
    TextView productName; 
    TextView productPrice; 
    TextView productBest; 
    TextView productURL; 
    ImageView symbol; 

    private ViewHolder(View v) { 
     super(v); 


     productImage = (ImageView) v.findViewById(R.id.productImage); 
     productName = (TextView) v.findViewById(R.id.productName); 
     productPrice = (TextView) v.findViewById(R.id.productPrice); 
     productBest = (TextView) v.findViewById(R.id.productBest); 
     productURL = (TextView) v.findViewById(R.id.productURL); 
     symbol = (ImageView) v.findViewById(R.id.symbol); 

     v.setOnClickListener(this); 
     v.setOnLongClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     TextView urlView = (TextView) view.findViewById(R.id.product_url); 
     String url = urlView.getText().toString(); 
     Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url)); 
     view.getContext().startActivity(intent); 
    } 

    @Override 
    public boolean onLongClick(View view) { 
     Toast.makeText(view.getContext(), "Long Click",Toast.LENGTH_SHORT).show(); 
     return false; 
    } 


} 

Mais dans le onClick(), le urlview est d'obtenir nulle. Je suppose que la vue provient d'un élément et non de la carte. Que dois-je faire alors?

Merci

Répondre

0

Le est que vous utilisez le paramètre en vue d'exécuter le findViewById.

Le paramètre view est la vue qui a été cliquée, donc ce n'est pas forcément le conteneur de vue.

Vous avez déjà exécuté findViewById, vous n'avez plus besoin de le faire.

Votre code corrigé est:

@Override 
public void onClick(View view) { 
    String url = productURL.getText().toString(); 
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url)); 
    view.getContext().startActivity(intent); 
} 
0

Essayez dans votre code.

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 

    ImageView productImage; 
    TextView productName; 
    TextView productPrice; 
    TextView productBest; 
    TextView productURL; 
    ImageView symbol; 
    // edited here 
    TextView urlView; 

    private ViewHolder(View v) { 
     super(v); 


     productImage = (ImageView) v.findViewById(R.id.productImage); 
     productName = (TextView) v.findViewById(R.id.productName); 
     productPrice = (TextView) v.findViewById(R.id.productPrice); 
     productBest = (TextView) v.findViewById(R.id.productBest); 
     productURL = (TextView) v.findViewById(R.id.productURL); 
     symbol = (ImageView) v.findViewById(R.id.symbol); 
     // edited here ,add findViewById here 
     urlView = (TextView) view.findViewById(R.id.product_url); 

     v.setOnClickListener(this); 
     v.setOnLongClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     // remove findViewById here 
     String url = urlView.getText().toString(); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     view.getContext().startActivity(intent); 
    } 

    @Override 
    public boolean onLongClick(View view) { 
     Toast.makeText(view.getContext(), "Long Click",Toast.LENGTH_SHORT).show(); 
     return false; 
    } 

}