我有一个自定义适配器和ListView活动。我需要实现全选/取消全选按钮。我使用MVC并有特殊的类来处理单词 - WordsHandler。因此,当用户按下按钮时,hendler将条件(布尔值 - 已选中/未选中)设置为单词并运行notifyDataSetChanged()方法。但这并不奏效。你能看到我的课程并告诉我我的错误吗?我是初学者,不知道哪里出错... 有趣的是,方法isAllSelected()返回不同的值,但适配器不会改变。
SettingsAdapter.java:
public class SettingsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private ArrayList<WordListModel> words;
private View view;
public SettingsAdapter(Context context, ArrayList<WordListModel> words) {
this.context = context;
this.words = words;
layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return words.size();
}
@Override
public Object getItem(int position) {
return words.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
CompleteListViewHolder viewHolder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.item, parent, false);
viewHolder = new CompleteListViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (CompleteListViewHolder) view.getTag();
}
viewHolder.checkBox.setText(words.get(position).getWord().toString());
if (words.get(position).isChecked())
viewHolder.checkBox.setChecked(true);
else
viewHolder.checkBox.setChecked(false);
return view;
}}
class CompleteListViewHolder {
public CheckBox checkBox;
public CompleteListViewHolder(View base) {
checkBox = (CheckBox) base.findViewById(R.id.checkBox);
}}
SettingsActivity.java:
public class SettingsActivityRTG extends Activity {
private WordsHandler wordsHandler;
private Context context;
private SettingsAdapter adapter;
private ListView listView;
private void enableActionBarBackButton() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
enableActionBarBackButton();
context = getApplicationContext();
wordsHandler = new WordsHandler(context);
adapter = new SettingsAdapter(this, wordsHandler.getWordsList());
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
try {
wordsHandler.writeStringsToFile(wordsHandler.getWordsList());
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_settings_action, menu);
if(wordsHandler.isAllSelected())
menu.findItem(R.id.action_select_all).setTitle(getString(R.string.deselall));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_select_all:
if(wordsHandler.isAllSelected()) {
wordsHandler.deselectAll();
item.setTitle(getString(R.string.selall));
} else {
wordsHandler.selectAll();
item.setTitle(getString(R.string.deselall));
}
adapter.notifyDataSetChanged();
return true;
default:
return super.onOptionsItemSelected(item);
}
}}
WordsHandler的一些方法:
private Context ctx;
private ArrayList<WordListModel> wordsArray;
private SharedPreferences sharedPreferences;
private String filePath;
private File file;
private File dir;
public void selectAll() {
for(WordListModel m : wordsArray)
m.setChecked(true);
}
public void deselectAll() {
for(WordListModel m : wordsArray)
m.setChecked(false);
}
public boolean isAllSelected() {
for(int i = 0; i < wordsArray.size(); i++)
if(wordsArray.get(i).isChecked() == false)
return false;
return true;
}
public WordsHandler(Context ctx) {
this.ctx = ctx;
filePath = Environment.getExternalStorageDirectory()+Const.PATH_TO_FILE;
dir = new File(filePath);
dir.mkdirs();
file = new File(dir,Const.FILE_NAME);
sharedPreferences = ctx.getSharedPreferences(Const.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
wordsArray = getWordsList();
}
答案 0 :(得分:0)
在致电notifyDataSetChanged
之前,您尚未更改适配器。您已更改wordsHandler
。在致电SettingsAdapter
notifyDataSetChanged
的内容