Displaying ArrayList items in ListView contains 2 textviews

后端 未结 3 1202
[愿得一人]
[愿得一人] 2021-01-26 13:25

I want to display the arrayList items in ListView which is having 2 different textViews. I am using ListViewCustomAdapter and getView(),getItem()... methods are there.

3条回答
  •  天涯浪人
    2021-01-26 13:44

    You have wrong implementation in some of the adapter methods.

    getItem() should return the object from your list at the position:

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }
    

    Then, in getView

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //  Create view
        //   ...
    
        String[] item = (String[])getItem(position);  //  Get the current object at 'position'
    
    
        //  Update your view here
        TextView t1=(TextView)List.findViewById(R.id.txtViewTitle);
        t1.setText(item[0]);
    
        TextView t2=(TextView)List.findViewById(R.id.txtViewDescription);
        t2.setText(item[1]);
    }
    

    If your want to display two different strings, I suggest you list should look like this

    [new String[]{"a1", "a2"}, new String[]{"b1", "b2"}, ...]
    

提交回复
热议问题