android 多个按钮响应,处理Android Recyclerview中的多个按钮单击并将响应存储在Array或ArrayList中...
我正在设计在线测验App。我设计了PlayQuiz.java文件如下:
public class PlayQuiz extends AppCompatActivity {
private RecyclerView recyclerView;
DataBaseHelper database;
private List quizList;
private QuizAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_quiz);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view_quiz_display);
database= new DataBaseHelper(PlayQuiz.this);
quizList= database.fillObjQuesList();
adapter=new QuizAdapter(quizList,getApplicationContext());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
});
}
}
现在,这是我的QuizAdapter.java文件
public class QuizAdapter extends RecyclerView.Adapter{
private List questionList;
private Context context;
public QuizAdapter(List questionList, Context context) {
this.questionList = questionList;
this.context = context;
}
@NonNull
@Override
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.quiz_display_format,parent,false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final CustomViewHolder holder, final int position) {
DmQuiz questionsList=questionList.get(position);
holder.tvquestion.getLayoutParams().width= LinearLayout.LayoutParams.WRAP_CONTENT;
holder.tvquestion.setText(questionsList.getQuestion());
holder.optA.setText(questionsList.getOpta());
holder.optB.setText(questionsList.getOptb());
holder.optC.setText(questionsList.getOptc());
holder.optD.setText(questionsList.getOptd());
holder.optA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundColor(context.getResources().getColor(R.color.colorButton));
holder.optA.setBackgroundResource(R.drawable.button_border); holder.optB.setBackgroundResource(R.drawable.button_border_unselected); holder.optC.setBackgroundResource(R.drawable.button_border_unselected); holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
holder.optD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border);
}
});
holder.tvClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.optA.setBackgroundResource(R.drawable.button_border_unselected);
holder.optB.setBackgroundResource(R.drawable.button_border_unselected);
holder.optC.setBackgroundResource(R.drawable.button_border_unselected);
holder.optD.setBackgroundResource(R.drawable.button_border_unselected);
}
});
}
@Override
public int getItemCount() {
return questionList.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder{
TextView tvquestion, tvClear;
Button optA,optB,optC,optD;
public CustomViewHolder(View itemView) {
super(itemView);
tvquestion=(TextView)itemView.findViewById(R.id.tvQuestion);
optA=(Button)itemView.findViewById(R.id.button1);
optB=(Button)itemView.findViewById(R.id.button2);
optC=(Button)itemView.findViewById(R.id.button3);
optD=(Button)itemView.findViewById(R.id.button4);
tvClear=(TextView)itemView.findViewById(R.id.tvClear);
}
}
public QuizAdapter(List questionList)
{
this.questionList=questionList;
}
public void setSearchOperation(List newList){
questionList= new ArrayList<>();
questionList.addAll(newList);
notifyDataSetChanged();
}
}
Recyclerview中的数据正在从SQLite数据库中显示。
现在我想处理/存储多个按钮上的点击并将其发送到远程MySql服务器。我很困惑,如何存储不同问题的回复?我应该使用ArrayList或其他东西,请帮助......
android 多个按钮响应,处理Android Recyclerview中的多个按钮单击并将响应存储在Array或ArrayList中...相关推荐
- android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法
Android Studio 运行按钮灰色的完美解决方法 今天新建项目的时候突然发现编译后运行按钮为灰色. 解决方案:第一步:点击图中的Add Configuration,出来如下界面 第二步:点+号 ...
- Android 系统(188)---Android开发:ListView、AdapterView、RecyclerView全面解析
Android开发:ListView.AdapterView.RecyclerView全面解析 目录 ListView.AdapterView.RecyclerView全面解析.png Adapter ...
- Android基于高德SDK的开发——自定义地图主题样式(悬浮按钮+底部弹窗)
日常的地图使用中,平台一般只会给我们提供地图的标准样式,造成了一定程度上的审美疲劳,那么如何实现地图的自定义样式呢?本文使用Android Studio 4.1,给开发者提供了一个基于高德地图SDK进 ...
- Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
运行有问题或需要全部源码请点赞关注收藏后评论区留言~~~ 一.循环视图RecyclerView 尽管ListView和GridView分别实现了多行单列和多行多列的列表,使用也很简单,可是它们缺少变化 ...
- android标题栏添加按钮_2019最新Android常用开源库总结(持续更新,建议收藏)
热文导读| 点击标题阅读 如何才能成为优秀的架构师? 23种设计模式及案例整理分享(建议收藏) 程序员的35个坏习惯,你有几条? 作者:欢子-3824 来源:https://blog.csdn.n ...
- Android 可长按拖拽的RecyclerView
近期项目遇到需要拖拽的RecyclerView来实现需求,首先考虑了ItemTouchHelper 这个类,但是后续使用发现无法把item视图拖出RecyclerView的视图范围,于是打算自定义Re ...
- android标题栏添加按钮_改善Android布局性能
布局是 Android 应用中直接影响用户体验的关键部分.如果实现不当,您的布局可能会导致应用界面缓慢且需要占用大量内存.Android SDK 包含一些工具,有助于您识别布局性能方面的问题,将这些工 ...
- android监听器在哪里创建,[转载]android开发中创建按钮事件监听器的几种方法
第一种:匿名内部类作为事件监听器类 Button button=(Button) findViewById(R.id.button); button.setOnClickListener(new On ...
- Android 5.0学习之ListView升级版RecyclerView
前言 大家都知道ListView在我们开发中很常用,Android5.0的到来,它给我们带来了一个替代 ListView的新的控件,也可以说是ListView的升级版,那就是RecyclerVi ...
最新文章
- java biginterger_Java BigInteger类知识点总结
- 01-CoreData简介
- SQL Server 动态生成数据库所有表Insert语句
- 第十届 蓝桥杯样题 ——代码填空
- 从0开始 Java实习 黑白棋
- Java学习日报 — 问题记录 — 2021/12/14
- java Date 转mysql timestamp 秒数不一致
- 有乳胶枕吗_乳胶枕怎么挑?网传的踩鸡蛋检测法,实际不靠谱
- 【生活相关】实验室专题研讨PPT模板说明备忘
- 信用卡前6位bin号代表什么
- 一键下载大学慕课等课程视频
- 手机拍证件照有什么诀窍
- 4510. 寻宝!大冒险!
- 影视广告之神器Cinema 4D
- [动态规划]基因序列相似性问题
- ubuntu16.04修改DNS永久生效
- 旧款华为笔记本解锁智慧语音教程
- PCA主成分分析法浅理解
- 数据结构面试题(含答案)
- bootloader的两种启动模式
热门文章
- VUE初始化一个项目
- VUE 使用中踩过的坑
- 使用MyBatista----上传图像
- Delphi XE7实现的任意位置弹出菜单
- Asp.net的HTTP请求处理过程
- .netframework3.5 中TimeZoneInfo 类的使用
- [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]38.隐蔽信道和侧信道的区别
- [Leetcode][第410题][JAVA][分割数组的最大值][动态规划][二分]
- PAT 1009 说反话
- POJ-3624 Charm Bracelet dp