gpt4 book ai didi

java - 从recycleview中获取cardview中textview的值

转载 作者:行者123 更新时间:2023-12-02 12:25:19 27 4
gpt4 key购买 nike

我有一个RecycleView,里面有卡片。每张卡都有公司和价格列表。

在我的 onBindViewHolder 中,我有一个点击事件,我想在其中获取 Cardview 内该行中 TextView 的价格。

每次我点击时,我总是会得到单张卡片内顶部商品的值(value)/价格,而永远不会得到我正在点击的商品的价格。

我用bindData方法的数据参数在Cardview中创建项目列表。

任何帮助将不胜感激。我只需要获取我点击的正确 TextView 的值。

public class StockCardAdapter extends RecyclerView.Adapter<StockCardAdapter.ViewHolder> {

public static class ViewHolder extends RecyclerView.ViewHolder {

protected RelativeLayout mCardBodyLayout;
protected TextView mTitleTextView;

public ViewHolder(View v) {
super(v);
mCardBodyLayout = (RelativeLayout) v.findViewById(R.id.card_body);
mTitleTextView = (TextView) v.findViewById(R.id.card_title);
}

public void bindData(StockCategoryModel data, Context ctx) {
this.mTitleTextView.setText(data.getCategoryName());

TableLayout tableLayout = new TableLayout(ctx);

int rows = data.getStockList().size();

for (int r = 0; r < rows; r++) {
TableRow row = new TableRow(ctx);

TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 16);
row.setLayoutParams(rowParams);

LinearLayout rl = new LinearLayout(ctx);
rl.setOrientation(LinearLayout.VERTICAL);

Integer priceColor = SharedUtilities.getColor(data.getStockList().get(r).priceChange, ctx);

//price row
LinearLayout priceLayout = new LinearLayout(ctx);
priceLayout.setOrientation(LinearLayout.HORIZONTAL);
priceLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
priceLayout.setWeightSum(4);

LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

final TextView price_text = new TextView(ctx);
price_text.setTag("priceTag");
price_text.setText(data.getStockList().get(r).price);
price_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
price_text.setTextColor(Color.BLACK);
price_text.setLayoutParams(textViewParams);
priceLayout.addView(price_text);

//company row
final TextView name_text = new TextView(ctx);
name_text.setText(data.getStockList().get(r).company);
name_text.setTextColor(Color.GRAY);
name_text.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
name_text.setMaxWidth(700);
name_text.setEllipsize(TextUtils.TruncateAt.END);
name_text.setMaxLines(1);
name_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

rl.addView(priceLayout);
rl.addView(name_text);
row.addView(rl);

tableLayout.setStretchAllColumns(true);
tableLayout.addView(row);
}
mCardBodyLayout.addView(tableLayout);
}

}

private List<StockCategoryModel> mDataset;
private Context mContext;

// Constructor
public StockCardAdapter(List<StockCategoryModel> dataset, Context ctx) {
this.mDataset = dataset;
this.mContext = ctx;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

// Create new views (invoked by the layout manager)
@Override
public StockCardAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
// create a new view
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);

ViewHolder vh = new ViewHolder(v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
});

holder.bindData(mDataset.get(position), mContext);
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}

}

最佳答案

您需要做的是为每一行分别设置一个点击监听器。

为什么总是获得第一行值?

这段代码,

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
});

为每个列表项(整个卡片)设置一个点击监听器。这意味着每次用户在卡片 View 边界内单击时,都会触发此回调。 但是,谁将成为v2?它始终是我们将监听器设置为的 View - 在本例中 - 整个卡片。

这意味着每次调用 v2.findViewWithTag("priceTag"); 时,您都会搜索整个卡片中具有标签“priceTag”的第一个子项 - 即“顶部”卡片中的项目。

如何解决这个问题?

如果您想确定哪个子项被单击 - 您必须直接为每个子项设置一个单击监听器。

作为示例,请尝试以下代码(请参阅添加注释):

public class StockCardAdapter extends 

RecyclerView.Adapter<StockCardAdapter.ViewHolder> {

public static class ViewHolder extends RecyclerView.ViewHolder {

protected RelativeLayout mCardBodyLayout;
protected TextView mTitleTextView;

public ViewHolder(View v) {
super(v);
mCardBodyLayout = (RelativeLayout) v.findViewById(R.id.card_body);
mTitleTextView = (TextView) v.findViewById(R.id.card_title);
}

public void bindData(StockCategoryModel data, Context ctx, View.OnClickListener listener) {
this.mTitleTextView.setText(data.getCategoryName());

TableLayout tableLayout = new TableLayout(ctx);

int rows = data.getStockList().size();

for (int r = 0; r < rows; r++) {
TableRow row = new TableRow(ctx);

TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams (TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
rowParams.setMargins(0, 0, 0, 16);
row.setLayoutParams(rowParams);

LinearLayout rl = new LinearLayout(ctx);
rl.setOrientation(LinearLayout.VERTICAL);

Integer priceColor = SharedUtilities.getColor(data.getStockList().get(r).priceChange, ctx);

//price row
LinearLayout priceLayout = new LinearLayout(ctx);
priceLayout.setOrientation(LinearLayout.HORIZONTAL);
priceLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
priceLayout.setWeightSum(4);

LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

final TextView price_text = new TextView(ctx);
price_text.setTag("priceTag");
price_text.setText(data.getStockList().get(r).price);
price_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
price_text.setTextColor(Color.BLACK);
price_text.setLayoutParams(textViewParams);
priceLayout.addView(price_text);

//company row
final TextView name_text = new TextView(ctx);
name_text.setText(data.getStockList().get(r).company);
name_text.setTextColor(Color.GRAY);
name_text.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
name_text.setMaxWidth(700);
name_text.setEllipsize(TextUtils.TruncateAt.END);
name_text.setMaxLines(1);
name_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

rl.addView(priceLayout);
rl.addView(name_text);
row.addView(rl);

tableLayout.setStretchAllColumns(true);
tableLayout.addView(row);

// *ADDED* set the listener directly to each row
row.setOnClickListener(listener);

}
mCardBodyLayout.addView(tableLayout);
}

}

private List<StockCategoryModel> mDataset;
private Context mContext;

// Constructor
public StockCardAdapter(List<StockCategoryModel> dataset, Context ctx) {
this.mDataset = dataset;
this.mContext = ctx;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

// Create new views (invoked by the layout manager)
@Override
public StockCardAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
// create a new view
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);

ViewHolder vh = new ViewHolder(v);
return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// *ADDED* Send the callback to the bind method
holder.bindData(mDataset.get(position), mContext, new View.OnClickListener() {
@Override public void onClick(View v2) {
final TextView textViewName = (TextView) v2.findViewWithTag("priceTag"); ;
final String priceTag = textViewName.getText().toString();
}
}));
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}

}

注意:

这不是处理 RecyclerView 的正确方法 - 您永远不想在数据绑定(bind)内创建新对象(在本例中为 new View.OnClickListener() {}) - 这会减少表现。但这是另一个问题:)

关于java - 从recycleview中获取cardview中textview的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com