【问题标题】:getView from Custom Adapter not updating the list items来自自定义适配器的 getView 不更新列表项
【发布时间】:2016-02-24 08:00:42
【问题描述】:

我正在开发 Android 中的 WordList Builder 应用程序。该应用程序从数据库中提取单词并显示它们。还有一个收藏夹选项卡,用于仅显示收藏夹。主页显示所有单词并在最喜欢的单词附近显示一个星号。当用户从收藏选项卡中取消选择一个单词并返回主页时,星号仍会显示在未选择的收藏单词上。然而,从数据库中获取的数据清楚地显示了更新。然后我将日志语句添加到自定义适配器的 getView 方法中,这里的项目列表仍然显示最初加载的单词列表。它不更新。

以下是我的自定义适配器代码:

public class WordListAdapter  extends ArrayAdapter<Word> {

    protected static final String LOG_TAG = WordListAdapter.class.getSimpleName();

    private ArrayList<Word> items;
    int layoutResourceId;
    private Context context;

    public WordListAdapter(Context context,int layoutResourceId,ArrayList <Word> items) {
        super(context,layoutResourceId,items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.items = items;
    }

    @Override
    public int getCount() {

        return items.size();
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        Log.i("WordListAdapter" ," NotifyDataSetChanged is called items size = " + this.items.size());
    }

    public void refreshList(ArrayList<Word> items) {
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        Log.i("WordListAdapter; " , " getView item size =" + this.items.size());
        Word wordListItems = this.items.get(position);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.wordlist_item, null);

        }

        TextView tvWordName = (TextView)convertView.findViewById(R.id.wordlist_word);
        tvWordName.setText(wordListItems.getWordName());
        TextView tvWordId = (TextView)convertView.findViewById(R.id.wordlist_id);
        tvWordId.setText(Integer.toString(wordListItems.getId()));
        CheckBox cbFav = (CheckBox)convertView.findViewById(R.id.checkBoxFav);
        Log.i("WordListAdapter :" , "Position= " + position + "  Word " + wordListItems);
        cbFav.setChecked(wordListItems.isFavourite());
        cbFav.setTag(position);

        return convertView;
    }
}

下面是主Activity的代码:

public class WordBuilder extends Activity {

    //private static SimpleCursorAdapter cursorAdapter = null;
    private static WordListAdapter wordListAdapter = null;
    private static ListView lv = null;
    private static WordListDBSupport dbSupport = null;
    TabBar tabbar;
    ArrayList<Word> wordList;
    Intent intent ;

    AdapterView.OnItemClickListener mOnItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            long viewId = view.getId();
            TextView tv = (TextView) view.findViewById(R.id.wordlist_id);
            int rowId = Integer.parseInt(tv.getText().toString());


            Log.i("RowId : ", String.format("%d", rowId));

            Bundle bundle = new Bundle();
            bundle.putInt("rowId", rowId);
            tabbar.getIntent().putExtra("rowId", rowId);
            switchTabInActivity(tabbar.WORDLISTDETAILACTIVITY_TAB);


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("WordBuilder ", " onCreate() ");
        setContentView(R.layout.activity_main);

        intent = getIntent();

        tabbar = (TabBar) this.getParent();


        dbSupport = new WordListDBSupport(this);
        lv = (ListView) findViewById(R.id.wordlist);

        //If getting tab 3, only the favourites
        wordList = new ArrayList<Word>();

        wordListAdapter = new WordListAdapter(this,R.id.wordlist, wordList);
        lv.setAdapter(wordListAdapter);

        lv.setOnItemClickListener(mOnItemClick);


    }

    public void switchTabInActivity(int indexTabToSwitchTo){
        tabbar.switchTab(indexTabToSwitchTo);
    }

    public void onResume() {
        super.onResume();
        Log.i("WordBuilder ", " onResume() ");
        updateCursorAdapter();
    }


    public void updateCursorAdapter() {
        //Query to get the updated list
        wordList.clear();
        Log.i("WordBuilder ", " updateCursorAdapter() wordList size =  " + wordList.size());
        wordList = dbSupport.getAllWords(intent.getIntExtra("isfavourite",0));
        Log.i("WordBuilder", "wordlist size after = " +wordList.size() );

        wordListAdapter.refreshList(wordList);
    }

    public void saveFavouritesHandler(View view) {

        //To be called on check or uncheck of the checkbox
        int position = (Integer)view.getTag();
        CheckBox cbFav = (CheckBox)view;
        Word wordToUpdate = wordList.get(position);
        int rowId = wordToUpdate.getId();
        Log.i("RowId isFav: ", String.format("%d ", rowId) + " isfav " + cbFav.isChecked() + " wordname= " + wordToUpdate.getWordName() + " Position = " + position);
        if (cbFav.isChecked()) {
            //save the selection in the database
            dbSupport.updateFavourite(rowId,1);
        } else {
            dbSupport.updateFavourite(rowId,0);
            Log.i("WordBuilder :" , " Position bring removed =" + position );
            if(intent.getIntExtra("isfavourite",0) == 1) {
                wordList.remove(position);
            }
        }
        updateCursorAdapter();
    }



}

我从收藏列表中删除了战斗并更新了数据库。 但是,当我返回列出所有单词的主选项卡时,battle 仍然被列为最爱。

【问题讨论】:

    标签: android adapter notifydatasetchanged


    【解决方案1】:

    当 yiu 添加或删除收藏图标时,您是否正在更新 ListView 适配器。如果它们是 2 个不同的活动,则必须更新 mainactivity 的 onResume 中的适配器

    【讨论】:

      【解决方案2】:

      尝试调用 adapter.notifyDataSetChanged()。 当你想刷新列表时。 希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多