ListFragment's ListView throws NullPointerException when I use my own ListAdapter

后端 未结 1 1301
生来不讨喜
生来不讨喜 2020-12-18 21:14

I\'m trying to display data from my database in a ListFragment. To do this, I have followed the same way when I use ListActivity, created my own adapter (adapter might look

相关标签:
1条回答
  • 2020-12-18 22:15

    You should return the view from getview() function. You are returning "Null".

            public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.company_list_item, parent, false);
            Company company = (Company) getItem(position);
    
            TextView nameView = (TextView) row.findViewById(R.id.CompanyListName);
            TextView telephoneView = (TextView) row.findViewById(R.id.CompanyListTelephone);
            TextView addressView = (TextView) row.findViewById(R.id.CompanyListAddress);
    
            nameView.setText(company.name);
            telephoneView.setText(company.telephone);
            addressView.setText(company.address);
    
            return row;//changed from null
        }
    

    Note: Use recycled view and Holders to make the listview scroll smooth. For beter understanding read this tutorial.

    0 讨论(0)
提交回复
热议问题