嗨所以我有一个列表视图,其中每个项目有4个radiobuttons。如果用户点击该项目,他们将被带到一个新活动,在那里他们可以更改单选按钮选择。但是,按下后退按钮返回列表视图后,它不会更新新选择的单选按钮。我尝试过notifyDataSetChanged(),但这样做不起作用。知道如何解决这个问题吗?
public class WordListActivity extends ListActivity {
DatabaseHandler db;
List<VocabWord> wordlist;
CustomWordListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_list);
db=new DatabaseHandler(this);
wordlist = db.getAllVocabWords();
adapter = new CustomWordListAdapter(this, (new ArrayList(wordlist)));
setListAdapter(adapter);
}
适配器类:
public class CustomWordListAdapter extends BaseAdapter {
private ArrayList listData;
private LayoutInflater layoutInflater;
public CustomWordListAdapter(Context context,ArrayList listData){
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final DatabaseHandler db= new DatabaseHandler(parent.getContext());
ViewHolder holder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.wordView=(TextView)convertView.findViewById(R.id.word_list_view_item);
holder.definitionView=(TextView)convertView.findViewById(R.id.definition_list_view_item);
holder.radiogroupView=(RadioGroup)convertView.findViewById(R.id.radioCategoryGroup_list);
holder.defaultButton = (RadioButton) convertView.findViewById(R.id.default_radiobutton_list);
holder.redButton = (RadioButton) convertView.findViewById(R.id.red_radiobutton_list);
holder.yellowButton = (RadioButton) convertView.findViewById(R.id.yellow_radiobutton_list);
holder.greenButton = (RadioButton) convertView.findViewById(R.id.green_radiobutton_list);
List<VocabWord>words1 = db.getAllVocabWords();
switch(words1.get(position).getCategory())
{
case 0:
holder.defaultButton.setChecked(true);
break;
case 1:
holder.redButton.setChecked(true);
break;
case 2:
holder.yellowButton.setChecked(true);
break;
case 3:
holder.greenButton.setChecked(true);
break;
}
holder.radiogroupView.setFocusable(false);
holder.radiogroupView.setFocusableInTouchMode(false);
convertView.setTag(holder);
} else{
holder = (ViewHolder) convertView.getTag();
}
final VocabWord word = (VocabWord)listData.get(position);
holder.wordView.setText(word.getWord());
holder.definitionView.setText(word.getDefinition());
holder.radiogroupView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedCategory) {
switch(checkedCategory)
{
case R.id.default_radiobutton_word:
word.setCategory(0);
db.updateRow(word.getId(), 0);
break;
case R.id.red_radiobutton_word:
word.setCategory(1);
db.updateRow(word.getId(), 1);
break;
case R.id.yellow_radiobutton_word:
word.setCategory(2);
db.updateRow(word.getId(), 2);
break;
case R.id.green_radiobutton_word:
word.setCategory(3);
db.updateRow(word.getId(), 3);
break;
}
}
});
return convertView;
}
static class ViewHolder{
TextView wordView;
TextView definitionView;
RadioGroup radiogroupView;
RadioButton defaultButton;
RadioButton redButton;
RadioButton yellowButton;
RadioButton greenButton;
}
}
在我的“新活动”中,我有一个监听器,它接收我放入数据库的新选择的单选按钮。
答案 0 :(得分:0)
你把notifyDataSetChanged()
放在哪里?您是否尝试在notifyDataSetChanged()
中致电onResume()
?
答案 1 :(得分:0)
因为当您按下后退按钮时,getView方法中当前位置的convertView实例不为空,因此单选按钮不会更新到最新状态。您应该将以下块代码移出代码块if (convertView == null) then { } else { }
。
List<VocabWord>words1 = db.getAllVocabWords();
switch(words1.get(position).getCategory())
{
case 0:
holder.defaultButton.setChecked(true);
break;
case 1:
holder.redButton.setChecked(true);
break;
case 2:
holder.yellowButton.setChecked(true);
break;
case 3:
holder.greenButton.setChecked(true);
break;
}
holder.radiogroupView.setFocusable(false);
holder.radiogroupView.setFocusableInTouchMode(false);
希望得到这个帮助。