刷新列表视图(自定义适配器)

时间:2018-02-09 07:17:23

标签: android listview

如何实时更新列表视图数据?它只是在重新创建时刷新(如果我完成活动并重新启动它)。但是否则它不会自动刷新。

adapter.notifydatasetchange();根本不起作用。我尝试了处理程序,按钮,列表项单击,选项菜单项单击,上下文菜单项单击,但我没有得到任何响应。我认为这种方法不会起作用。

我的活动

String [] titles = {"List option 1","List option 2","List option 3"};
String [] descriptions = {updateDescription1(),updateDescription2(),updateDescription3()};
ListView lv;

public String updateDescription() {
    //live clock!!!!!!!!!!
    long time = System.currentTimeMillis();
    @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
    return sdf2.format(time);
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        lv = (ListView) findViewById(R.id.listView);
        final UserAdapter adapter = new UserAdapter(getApplicationContext(), titles, descriptions);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
     });
}

我的适配器类

class UserAdapter extends ArrayAdapter {
    String[] titleArray;
    String[] descriptionArray;

public UserAdapter(Context context, String[] titles1, String [] description1) {
    super(context, R.layout.no_icon_list_row, R.id.noIconListDescriptions, titles1);
    this.titleArray = titles1;
    this.descriptionArray = description1;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.no_icon_list_row,parent,false);
    TextView myTitle = (TextView) row.findViewById(R.id.noIconListTitles);
    TextView myDescription = (TextView) row.findViewById(R.id.noIconListDescriptions);
    myTitle.setText(titleArray[position]);
    myDescription.setText(descriptionArray[position]);
    return row;
   }
}

现在,我无法更新说明文字;我该如何实时更新它们?

4 个答案:

答案 0 :(得分:1)

public View getView(int position, View convertView, ViewGroup parent) {

View row = inflater.inflate(R.layout.no_icon_list_row,parent,false);

return row;
   } 
} 

不对。你可以在下面修改:

public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null){
      convertView = inflater.inflate(R.layout.no_icon_list_row,parent,false);
}

TextView myTitle = (TextView) convertView.findViewById (R.id.noIconListTitles);


return convertView, 
   } 

持续。如果你想要刷新数据只需调用adapter.notifyDataSetChanged。希望帮助你。

答案 1 :(得分:0)

您可以在以下数组中添加数据,并在listview上调用notifyDataSetChanged方法

String [] titles = {"List option 1","List option 2","List option 3" , "List option 4" , "List option 5"};
String [] descriptions = {updateDescription1(),updateDescription2(),updateDescription3() , updateDescription4() , updateDescription5()};

adapter.notifyDataSetChanged();

答案 2 :(得分:0)

public String updateDescription() {
 //my update text data method
 adapter.notifyDataSetChanged();
 return text;
 }

这将通知您的数据已被更改并将绘制所有项目。

我建议使用recyclerview,因为您可以指定哪个项目已更改,并且只重绘该项目(重新绑定)。使用notifyItemChanged(position)

答案 3 :(得分:0)

您需要告诉适配器您进行了更改。执行逻辑更新后,在适配器上调用此方法

adapter.notifyDataSetChanged()