我在列表视图适配器中使用单选按钮,我想要的是选择/取消选择我单击的单选按钮。如何选择/取消选择列表视图阵列适配器的RadioButton android
-3
A
回答
0
将你的单选按钮状态保存在列表中,或者如果在这种情况下只有一个选择,则可以将特定位置保存在一个int变量中,并在选择adapter.notifyDataSetChanged()
之后更新所选变量的值。您需要编写代码getView
以反映更改。
粗糙例如:(我老了现在的ListView(检查recyclerview),但你的adpater应该是这样的)
Adapter{
int selectedPosition = -1; // initially nothing selected
getView(..,..., int position){
if(selectedPosition==position){
holder.radioButton.setChecked(true);
}else{
holder.radioButton.setChecked(false);
}
holder.radioButton.setTag(position);
holder.radioButton.onClick(){
selectedPosition = (Integer)holder.radioButton.getTag();
notifyDataSetChanged();
}
)
static class Holder {
RadioButton radioButton;
}
0
添加clickListener到radionButton所以如果你点击单选按钮会取消选择,如果它被选中。 (默认情况下它会在点击进行检查,如果它不选中)
radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (radioButton.isChecked()) {
radioButton.setChecked(false);
}
}
});
0
在这一步首先我们得到的Button
和ListView
参考。之后,我们为问题创建一个字符串数组,然后设置adapter
来填充ListView
中的数据。最后,我们在Button
上执行setOnClickListener
事件,因此无论何时用户单击Button
时,使用Toast
显示所选问题的答案。
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView simpleList;
String[] questions;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the string array from string.xml file
questions = getResources().getStringArray(R.array.questions);
// get the reference of ListView and Button
simpleList = (ListView) findViewById(R.id.simpleListView);
submit = (Button) findViewById(R.id.submit);
// set the adapter to fill the data in the ListView
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
simpleList.setAdapter(customAdapter);
// perform setOnClickListerner event on Button
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = "";
// get the value of selected answers from custom adapter
for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
}
// display the message on screen with the help of Toast.
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
In this step we create a custom adapter class and extends BaseAdapter in this class. In this step we firstly set the data in the list and then perform setOnCheckedChangeListener event on RadioButton.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
// initialize arraylist and add static string for all the questions
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("Not Attempted");
}
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return questionsList.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_items, null);
// get the reference of TextView and Button's
TextView question = (TextView) view.findViewById(R.id.question);
RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
RadioButton no = (RadioButton) view.findViewById(R.id.no);
// perform setOnCheckedChangeListener event on yes button
yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "Yes");
}
});
// perform setOnCheckedChangeListener event on no button
no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set No values in ArrayList if RadioButton is checked
if (isChecked)
selectedAnswers.set(i, "No");
}
});
// set the value in TextView
question.setText(questionsList[i]);
return view;
}
}
0
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Radio button
RadioButton rb1=(RadioButton)findViewById(R.id.rdb1);
if (rb1.isChecked()){
Toast.makeText(MainActivity.this, "1 option selected", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "2 option selected", Toast.LENGTH_LONG).show();
}
}
});
相关问题
- 1. 选择Android列表视图阵列适配器
- 2. 选择一个项目内的列表视图适配器android
- 3. android-如何取消选中的已选中的checkedtextview选择列表视图
- 4. 如何取消选中阵列适配器中的复选框
- 5. JSON阵列选择器视图iPhone
- 6. 如何在列表视图中选择适当的复选框
- 7. 如何取消选择kendo列表视图中的项目
- 8. 如何选择和取消选择Android
- 9. 如何在选择1时取消选择其他列表框
- 10. 不允许在列表框中取消选择/取消选择
- 11. 列表视图的固定选择器
- 12. Android - 在列表视图中选择TextView
- 13. 如何选择列表视图
- 14. 选择匹配阵列总
- 15. 基于微调器选择的Android列表视图列
- 16. 选择阵列
- 17. 如何选择选择列表选项
- 18. 选择空白行从列表视图中取消选择项目
- 19. 在列表视图中滚动时取消选择项目选择
- 20. 如何选择和取消选择缩略图图像和所选图像存储阵列中的iphone
- 21. 随机选择视图阵列
- 22. 从多个选择列表中取消选择选项
- 23. 与日期选择器的RadioButton选择
- 24. wpf - 从嵌套列表视图选择项目也选择父列表视图
- 25. 取消选择列闪亮
- 26. 如何获取整个列表项的列表选择器?
- 27. 选择,在列表视图(刷卡-列表视图)行,停留在状态下按下重新instanciating适配器
- 28. 自定义列表视图和适配器和列表视图选择去疯狂?
- 29. Android列表视图(阵列适配器)自定义滚动
- 30. 不能“取消选择”列表视图项
那你试试?并分享一些代码 –
okey,我尝试使用适配器类中的单选按钮,但是如果一旦选中,我无法选中/取消选中单选按钮。 – Shivangi
在下面提供了一个答案。 –