当我单击按钮“ btn”时,我想从活动TextView
更新适配器(textViewNum)中的SlotsActivity
我如何访问TextView
?
请记住,listItems
不为空。
这是我的Activity
代码:
public class SlotsActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private MyAdapter adapter;
private List<ListItem> listItems;
private Button btn;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slots);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
....
btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//i want to update the textview here
}
});
}
这是我的Adapter
:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems ;
private Context context;
public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
....
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ListItem listItem= listItems.get(position);
//**I want to update this textView**
holder.textViewNum.setText("N° slot : "+listItem.getNum()+"");
}
....
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView textViewNum;
public ViewHolder(View itemView) {
super(itemView);
textViewNum = itemView.findViewById(R.id.textViewNum);
}
}
}
答案 0 :(得分:0)
一种方法是将ViewHolder
用作SlotsActivity
的子类,并在父类中声明TextView
变量。
喜欢
public class SlotsActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private MyAdapter adapter;
....
TextView txtView;
.....
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textViewNum);
}}}
通过这种方式,您可以访问并更改TextView
的值。
答案 1 :(得分:0)
尝试这个想法
// i is index of item u want to change text
View v = getViewByPosition(i,listView);
// then check if i is odd set background color with what u want
v.findViewById(R.id.text_view).text("what u want");
public View getViewByPosition(int position, RecyclerView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition =firstListItemPosition +
listView.getChildCount() - 1;
if (position < firstListItemPosition || position > lastListItemPosition ) {
return listView.getAdapter().getView(position,
listView.getChildAt(position), listView);
} else {
final int childIndex = position - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
答案 2 :(得分:0)
adapter = new MyAdapter(listItems,SlotsActivity.this);
这样做,该值将存储在arraylist中。
希望您在代码中添加了以下行。
capabilities
希望有帮助。
答案 3 :(得分:0)
https://stackoverflow.com/a/52307995/8316499
检查此链接
只需通过适配器编写的以下方法传递您的值
public void addExploreItem(String item, Arraylist<String> textviewvalues) {
this.textviewvalues = textviewvalues
count = Integer.parseInt(item);
notifyDataSetChanged();
}