我在列表視圖適配器中使用單選按鈕,我想要的是選擇/取消選擇我單擊的單選按鈕。如何選擇/取消選擇列表視圖陣列適配器的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
- 4. 列表視圖有多個項目選擇和取消選擇
- 5. 選擇/取消選擇列表視圖中的所有項目
- 6. android-如何取消選中的已選中的checkedtextview選擇列表視圖
- 7. 如何取消選中陣列適配器中的複選框
- 8. android:刪除列表視圖選擇器
- 9. JSON陣列選擇器視圖iPhone
- 10. 如何在列表視圖中選擇適當的複選框
- 11. 自定義列表視圖適配器行選擇
- 12. 在列表視圖中滾動時取消選擇視圖選擇
- 13. 如何取消選擇列表視圖中的過濾項目?
- 14. 如何取消選擇kendo列表視圖中的項目
- 15. 選擇/取消選擇列表視圖中的所有複選框與分頁
- 16. 如何選擇和取消選擇Android
- 17. 當選擇其他列表框時取消選擇列表框
- 18. 列表視圖選擇
- 19. 如何在選擇1時取消選擇其他列表框
- 20. 不允許在列表框中取消選擇/取消選擇
- 21. 列表視圖的固定選擇器
- 22. Android - 在列表視圖中選擇TextView
- 23. 取消選擇,並從陣列
- 24. 如何選擇列表視圖
- 25. 如何在選擇列表視圖項
- 26. 選擇匹配陣列總
- 27. 選擇/取消選擇多個列表中的複選框
- 28. 基於微調器選擇的Android列表視圖列
- 29. 陣列選擇
- 30. 選擇陣列
那你試試?並分享一些代碼 –
okey,我嘗試使用適配器類中的單選按鈕,但是如果一旦選中,我無法選中/取消選中單選按鈕。 – Shivangi
在下面提供了一個答案。 –