2014-07-16 44 views
-2

我有一個自定義適配器和活動與ListView。我需要實現Select all/deselect all按鈕。我使用MVC,並有特殊的類來處理單詞 - WordsHandler。所以,當用戶按下按鈕時,hendler設置條件(布爾值 - 選中/未選中)並運行notifyDataSetChanged()方法。但這不起作用。你能看到我的課程並告訴我我的錯誤嗎?我是一個初學者,不知道錯誤在哪裏... 有趣的是,方法isAllSelected()返回不同的值,但適配器不會改變。自定義適配器與notifyDataSetChanged不工作

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; 
}} 

類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

你打電話notifyDataSetChanged之前,你有沒有改變你的適配器。您已更改wordsHandler。在致電之前更改SettingsAdapter的內容notifyDataSetChanged

+0

如何更改適配器? – wolltone

+0

您必須跟蹤適配器中的選定狀態。您目前正在'WordsHandler'中執行此操作。您可以在您的適配器中添加一個'ArrayList '並在'onOptionsItemSelected'中更改該變量。然後,您的適配器更改並且'notifyDataSetChanged'應該可以工作 – 0xDEADC0DE

+0

謝謝!它的工作! – wolltone

相關問題