gpt4 book ai didi

java - ListView 未使用按钮更新

转载 作者:行者123 更新时间:2023-12-01 04:32:25 26 4
gpt4 key购买 nike

嗨,我有一个 ListView ,其中每个单独的项目都有 4 个单选按钮。如果用户单击该项目,他们将进入一个新 Activity ,可以在其中更改单选按钮选择。但是,当您按后退按钮返回 ListView 后,它不会更新新选择的单选按钮。我尝试过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;





}
}

在我的“新 Activity ”中,我有一个监听器,它接收我放入数据库中的新选择的单选按钮。

最佳答案

因为当您按下后退按钮时,getView方法中当前位置的convertView实例不为null,因此单选按钮不会更新到最新状态。您应该将以下 block 代码移出代码块 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);

希望这有帮助。

关于java - ListView 未使用按钮更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17849878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com