2017-10-09 35 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越來越空。我認爲這是來自一個元素而不是來自卡片。那我該怎麼辦?

謝謝

回答

0

這就是你正在使用view參數來執行findViewById。

view參數是被點擊的視圖,所以它必然不是視圖容器。

你已經執行了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; 
    } 

}