简体   繁体   中英

Unconditional layout inflation from view adapter: Should use View Holder pattern -crash the app

i have warning that cause crash my app.

it is the warning ::

(((( Unconditional layout inflation from view adapter: Should use View Holder pattern (use recycled view passed into this method as the second parameter) for smoother scrolling))))

it is my code ::

public class ContactsAdapter extends ArrayAdapter<Contacts> {

    Context mCtx;
    int layoutRes ;
    List<Contacts> contactsList;


    public ContactsAdapter(Context mCtx, int layoutRes, List<Contacts> 
                   contactsList)
       {

        super(mCtx, layoutRes, contactsList);

        this.mCtx = mCtx ;
        this.layoutRes = layoutRes ;
        this.contactsList = contactsList ;

    }

    @NonNull
    @Override
      public View getView(int position, @Nullable View convertView, 
                            @NonNull 
      ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);

            View view = inflater.inflate(layoutRes, null);

            TextView textView = view.findViewById(R.id.textViewContactMessage);

            Contacts contacts = contactsList.get(position);

            textView.setText(contacts.getPhone());

            textView.setText(contacts.getContext());

            return view;
        }

    }

warning is in line ::: View view = inflater.inflate(layoutRes, null);

and it is my MessageActivity code ::

public class MessageActivity extends AppCompatActivity {


    List<Contacts> contactsList;
    ListView listView;

    SQLiteDatabase mDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);

        mDatabase = openOrCreateDatabase(MainActivity.DATABASE_NAME, MODE_PRIVATE, null);

        contactsList = new ArrayList<>();
        listView = (ListView) findViewById(R.id.listViewContactMessage);

        loadContact_dbFromDatabase();
    }


    private void loadContact_dbFromDatabase(){
        String sql = " SELECT * FROM contact_message";
        Cursor cursor = mDatabase.rawQuery(sql, null);

        if (cursor.moveToFirst()){

            do {
                contactsList.add(new Contacts (
                    cursor.getInt(0),
                    cursor.getString(1),
                    cursor.getString(2)
                ));

            }while (cursor.moveToNext());

                ContactsAdapter adapter = new ContactsAdapter(this, R.id.textViewContactMessage, contactsList);
                listView.setAdapter(adapter);
        }
    }
}

i know that other said this question but i do not know how to change my code!!!

can any say to me what should i do??!!

I think you are creating another view inside the convertview,that's why you are getting error here.

 View view = inflater.inflate(layoutRes, null);

Try the below code where I have put the listview row in a static inner class.

    @NonNull
    @Override
      public View getView(int position, @Nullable View convertView,               
      @NonNull ViewGroup parent) {
        final Holder viewHolder;
        if (convertView == null) {

            viewHolder = new Holder();
            convertView = inflater.inflate(layoutRes, null);
            viewHolder.textView = convertView.findViewById(R.id.textViewContactMessage);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (Holder) convertView.getTag();   
        }
        return convertView;
    }
    private static class Holder {
        TextView textView;
    }
}

Try to initialize your layout inflater in constructor and not in getView() .

LayoutInflater inflater;
public ContactsAdapter(Context mCtx, int layoutRes, List<Contacts> 
                   contactsList)
       {
    super(mCtx, layoutRes, contactsList);

    this.mCtx = mCtx ;
    this.layoutRes = layoutRes ;
    this.contactsList = contactsList ;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

in getView() , inflate your layout

view = inflater.inflate(layoutRes, null);

and also you have to use view holder as @Padmini S suggested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM