【问题标题】:Update listview?更新列表视图?
【发布时间】:2015-01-14 04:50:17
【问题描述】:

如何更新我的列表视图,每次我将图像添加到 sdcard 时,它都没有显示我需要再次运行我的应用程序才能看到图片。你能帮我吗谢谢。

@Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acmain);

    sharedpreferences = this.getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);
    frameChooserGV = (ListView) findViewById(R.id.listview);
    utils = new UtilsList(ListViewImage.this.getApplicationContext());
    InitializeGridLayout();
    framePaths = utils.getFilePaths();
    frameChooserAdapter = new ListViewImageAdapter(
            ListViewImage.this.getApplicationContext(), framePaths,
            columnWidth);
    frameChooserAdapter.notifyDataSetChanged();
    frameChooserGV.setAdapter(frameChooserAdapter);
    frameChooserGV.setOnItemClickListener(this);
}

public void InitializeGridLayout() {
    Resources r = getResources();
    float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            AppConstant.GRID_PADDING, r.getDisplayMetrics());
    columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // TODO Auto-generated method stub
    Log.e("" + frameChooserGV.getPositionForView(view), "" + position);
    Intent b_intent = new Intent(getApplicationContext(),
            com.example.customize.CustomizeNumberDisplay.class);
    b_intent.putExtra("position", position);
    startActivity(b_intent);

}

【问题讨论】:

  • 调用 notifyDataSetChanged() 以在更新填充列表的数据后刷新您的列表。
  • 我试着把它写成“frameChooserAdapter.notifyDataSetChanged();”但它不起作用。
  • 你把那行代码放在哪里了?您需要更新填充列表的数据,然后刷新列表视图
  • 我只是把它放在 onCreate 中,我不知道我应该把它放在哪里。呵呵
  • 读取活动生命周期。 onCreate 在活动的生命周期中被调用一次,直到它被销毁。所以预计它会以这种方式工作。您需要决定如何刷新。学习活动生命周期

标签: android image listview


【解决方案1】:

使用这个

myListView.invalidateViews();在 Resume 方法中,这将有助于在 listview android 中再次创建视图。或者也可以在您的适配器上调用notifyDataSetChanged();

希望这会有所帮助

【讨论】:

    【解决方案2】:

    修改适配器中的数据后,您需要在适配器对象上调用notifyDataSetChanged()

    来源:How to refresh Android listview?

    还有:

    Android ListView not refreshing after notifyDataSetChanged

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2021-07-08
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多