What is difference between android.R.layout.simple_list_item_1 and android.R.layout.simple_list_item_2

后端 未结 6 1919
南旧
南旧 2020-12-08 15:00

Can anyone explain android.R.layout.simple_list_item_1 and android.R.layout.simple_list_item_2 in arrayadapter in android.

I know in android.R.layout.simple_list_ite

6条回答
  •  温柔的废话
    2020-12-08 15:44

    I found this to be the simplest answer to your question:

    ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        TextView text1 = (TextView) view.findViewById(android.R.id.text1);
        TextView text2 = (TextView) view.findViewById(android.R.id.text2);
    
        text1.setText(person[position].getName());
        text2.setText(person[position].getAge());
        return view;
      }
    };
    

    If you didn't notice: the trick is to supply android.R.id.text1 as (principally unneccessary) parameter to ArrayAdapter, otherwise the call to super will cause an exception.

    Also, this solution does not need an Inflater or make use of TwoLineListItem, which was deprecated in API 17.

提交回复
热议问题