How can I update a single row in a ListView?

前端 未结 11 1974
时光说笑
时光说笑 2020-11-22 17:07

I have a ListView which displays news items. They contain an image, a title and some text. The image is loaded in a separate thread (with a queue and all) and w

11条回答
  •  伪装坚强ぢ
    2020-11-22 17:23

    I found the answer, thanks to your information Michelle. You can indeed get the right view using View#getChildAt(int index). The catch is that it starts counting from the first visible item. In fact, you can only get the visible items. You solve this with ListView#getFirstVisiblePosition().

    Example:

    private void updateView(int index){
        View v = yourListView.getChildAt(index - 
            yourListView.getFirstVisiblePosition());
    
        if(v == null)
           return;
    
        TextView someText = (TextView) v.findViewById(R.id.sometextview);
        someText.setText("Hi! I updated you manually!");
    }
    

提交回复
热议问题