如何在自定义适配器中刷新listview?

时间:2016-05-26 11:52:43

标签: android listview

我使用从数据库填充的自定义适配器创建了自定义列表视图。每当数据库中的数据发生变化时,我都无法更新我的列表视图。我已经尝试过使用notifysetdatachanged()但它没有用。也许是因为我以错误的方式推出了代码。请帮我解决这个问题。感谢。

ContactActivity.java



    public  void getImages2(){
        class GetImages2 extends AsyncTask<Void,Void,Void>{
            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(Void v) {
                super.onPostExecute(v);
                loading.dismiss();
                customList = new CustomList(ContactActivity.this,GetAlImages2.imageURLs,GetAlImages2.bitmaps,GetAlImages2.code,GetAlImages2.name,GetAlImages2.phone,GetAlImages2.email);
                listView.setAdapter(customList);
                customList.notifyDataSetChanged();
  
                }
            @Override
            protected Void doInBackground(Void... voids) {
                try {
                    getAlImages2.getAllImages2();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }
        GetImages2 getImages = new GetImages2();
        getImages.execute();
    }
&#13;
&#13;
&#13;

CustomList.java

&#13;
&#13;
public class CustomList extends ArrayAdapter<String> {
    private Bitmap[] bitmaps;
    private String[] urls;
    private String[] code;
    private String[] name;
    private String[] email;
    private String[] phone;

    private Activity context;

    public CustomList(Activity context,  String[] urls, Bitmap[] bitmaps, String[] code, String[] name, String[] phone, String[] email) {
        super(context, R.layout.activity_contact_item, urls);
        this.context = context;
        this.urls= urls;
        this.bitmaps= bitmaps;
        this.code= code;
        this.name= name;
        this.phone= phone;
        this.email =  email;
    }

}
&#13;
&#13;
&#13;

[编辑]

最后,我找到了解决问题的方法。我的listview应该首先检查listview的适配器是否为空。我将此代码添加到我的onPostExecute中。谢谢你的帮助。

&#13;
&#13;
customList = new CustomList(ContactActivity.this,GetAlImages2.imageURLs,GetAlImages2.bitmaps,GetAlImages2.code,GetAlImages2.name,GetAlImages2.phone,GetAlImages2.email);
if(listView.getAdapter() == null){ //Adapter not set yet.
   listView.setAdapter(customList);
}
else{ //Already has an adapter
   listView.setAdapter(customList);
   customList.notifyDataSetChanged();
   listView.invalidateViews();
   listView.refreshDrawableState();
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

试试这个

onResume()内添加此

if (listvew!= null) {
  listvew.invalidateViews();
}
adapter.notifyDataSetChanged();

答案 1 :(得分:1)

请尝试这些

new Handler().post(new Runnable() {
    @Override
    public void run() {
        // Code here will run in UI thread
         adapter.notifyDataSetChanged();
    }
});

OR

someActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
         adapter.notifyDataSetChanged();
        }
});

答案 2 :(得分:0)

我认为你需要对包含listview的活动实现加载器回调..你可以按照这个答案 Listview not updating after database update and adapter.notifyDataSetChanged();

更常见的编码方式是:

将此界面添加到您的活动中:

public class MyActivity extends Activity implementsLoaderManager.LoaderCallbacks<Cursor>

onCreate中,设置适配器(注意此时光标为空):

String[] from = new String[] { TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_ID};
int[] to = new int[] { R.id.label };
adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from, to, 0);  //Note that the cursor is null
lw.setAdapter(adapter);

启动加载器:

getLoaderManager().initLoader(0, null, this);

这会在后台线程中调用onCreateLoader(因此,如果您的查询长时间运行,则不会阻止UI线程)。完成后,在UI线程上调用onLoadFinished,您可以在其中交换新光标。

执行删除或更新后,重新启动加载程序:

getLoaderManager().restartLoader(0, null, this);

这会调用onLoaderReset,从适配器中删除现有游标,然后再次调用onCreateLoader以交换新游标。

最后添加以下方法:

public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
    String[] from = new String[] { TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_ID};
    String where = TodoTable.COLUMN_DELETED + " = ?";

    Loader<Cursor> loader = new CursorLoader(this, TodoContentProvider.CONTENT_URI, from, where, new String[] {"0"}, null);     
    return loader;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
    adapter.swapCursor(cursor);
}

public void onLoaderReset(Loader<Cursor> loader)
{
    adapter.swapCursor(null);
}