2017-10-09 12 views
1

私はカードのリストを作成しています。私は、クリックされたカードが持っているTextView内のテキストを取得したいと思います。 カード内のTextViewを取得

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


} 

しかしのonClick()

は、urlViewがnullになっている:私のアダプタで

私はこれを持っています。私はカードからのものではなく要素からのものであると考えています。私はどうしたらいいですか?

は、ザ・あなたはfindViewByIdを実行するビューパラメータを使用していることである

答えて

0

ありがとうございます。

ビューパラメータは、クリックされたビューであり、必ずしもビューコンテナではありません。

あなたは既にfindViewByIdを実行しました。あなたはもうこれを行う必要はありません。

あなたの修正コードは次のとおりです。

@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

はあなたのコードでこれを試してみてください。

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

} 
関連する問題