添加数据后,RecyclerView不会更新

时间:2016-02-09 06:33:17

标签: android-recyclerview notifydatasetchanged

我从ListView迁移到RecyclerView,但在SQLite中输入一些数据后,我的列表没有使用notifyDataSetChanged()更新;所以我总是要调用setAdapter()方法;

我已将我的代码简化为在此处发布。任何人都可以帮我修复notifyDataSetChanged();请在我的代码中工作吗?

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

private List<Model> model;
private Context context;

public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
    this.model = new ArrayList<>(model);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_layout, parent, false);
    return new ViewHolder(context, view, model);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Model model = model.get(position);
    holder.bind(model);
}

@Override
public int getItemCount() {
    return model.size();
} }

ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private List<Model> model = new ArrayList<>();
private Context context;

private TextView textViewGroup;
private TextView textViewName;

public ViewHolder(Context context, View itemView, List<Model> model) {
    super(itemView);

    this.model = model;
    this.context = context;

    itemView.setOnClickListener(this);

    textViewGroup = (TextView) itemView.findViewById(R.id.textViewGroup);
    textViewName = (TextView) itemView.findViewById(R.id.textViewName);

}

public void bind(Model model) {
    textViewGroup.setText(model.getGroup());
    textViewName.setText(model.getName());
    }
}

@Override
public void onClick(View view) {
    Model model = this.model.get(getAdapterPosition());
    Intent intent = new Intent(this.context, ShowGroup.class);
    intent.putExtra("DATA", model);
    intent.putExtra("POSITION", getAdapterPosition());
    ((Activity) this.context).startActivityForResult(intent, 7);

    Log.i(">>", "_IDHOLDER: " + model.getId());
    Log.i(">>", "_POSITION: " + getAdapterPosition());
    Log.i(">>", ".");
} }

模型

public class Model implements Serializable {

public static final String TABLE = "GROUP_NAME";
public static final String ID = "_ID";
public static final String GROUP = "GROUP";
public static final String NAME = "NAME";

private long id;
private String group;
private String name;

public Model() {
    setId(0);
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getGroup() {
    return group;
}

public void setGroup(String group) {
    this.group = group;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
} }

存储库

public class Repository {

private SQLiteDatabase db;

public Repository(SQLiteDatabase db) {
    this.db = db;
}

private ContentValues fillModelValues(Model model) {
    ContentValues values = new ContentValues();
    values.put(Model.GROUP, model.getGroup());
    values.put(Model.NAME, model.getName());

    return values;
}

public void insert(Model model) {
    ContentValues values = fillModelValues(model);
    db.insertOrThrow(Model.TABLE, null, values);
}

public List<Model> consDataBase(Context context) {

    List<Model> listModel = new ArrayList<>();

    Cursor cursor = db.query(Model.TABLE, null, null, null, null, null, null);

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();

        do {
            Model model = new Model();
            model.setId(cursor.getLong(cursor.getColumnIndex(Model.ID)));
            model.setGroup(cursor.getString(cursor.getColumnIndex(Model.GROUP)));
            model.setName(cursor.getString(cursor.getColumnIndex(Model.NAME)));

            listModel.add(model);

        } while (cursor.moveToNext());
    }

    cursor.close();

    return listModel;
} }

MainActivity

public class MainActivity extends AppCompatActivity {

private List<Model> listModel;
private LinearLayoutManager layoutManager;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;

private FloatingActionButton fab;

private DataBase dataBase;
private SQLiteDatabase db;
private Repository repository;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layoutManager = new LinearLayoutManager(this);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(layoutManager);

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(MainActivity.this, AddGroup.class), 4);
        }
    });

    try {
        dataBase = new DataBase(this);
        db = dataBase.getWritableDatabase();
        repository = new Repository(db);

        listModel = repository.consDataBase(context);
        recyclerViewAdapter = new RecyclerViewAdapter(context, listModel);
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.setHasFixedSize(true);

    } catch (SQLException e) {

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (db != null) {
        db.close();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 4) {

        listModel = repository.consDataBase(context);
        recyclerViewAdapter = new RecyclerViewAdapter(context, listModel);
        recyclerView.setAdapter(recyclerViewAdapter);       
    }
} }

ADDGROUP

public class AddGroup extends AppCompatActivity {

private EditText editTextGroup;
private EditText editTextName;

private DataBase dataBase;
private SQLiteDatabase db;
private Repository repository;
private Model model;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_group);

    editTextGroup = (EditText) findViewById(R.id.editTextGroup);
    editTextName = (EditText) findViewById(R.id.editTextName);

    try {
        dataBase = new DataBase(this);
        db = dataBase.getWritableDatabase();
        repository = new Repository(db);
        model = new Model();

    } catch (SQLException e) {

    }
}

private void saveModel() {
    try {
        model.setGroup(editTextGroup.getText().toString().trim());
        model.setName(editTextName.getText().toString().trim());
        if (model.getId() == 0) {
            repository.insert(model);
        }

    } catch (Exception e) {

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (db != null) {
        db.close();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_add_group, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_save) {
        saveModel();            

    }
    return super.onOptionsItemSelected(item);
} }

3 个答案:

答案 0 :(得分:3)

它现在正在工作

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

    private List<Model> model;
    private Context context;

    public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
        this.model = new ArrayList<>(model);
    }

    ...

    public void notify(List<Model> list) {
        if (model != null) {
        model.clear();
        model.addAll(list);

        } else {
            model = list;
        }
        notifyDataSetChanged();
    }
}

然后在onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 4) {
    listModel = repository.consDataBase(this);
        recyclerViewAdapter.notify(listModel);
    }
}

谢谢你:)

答案 1 :(得分:2)

为了使代码正常工作,您需要更改Adapter构造函数实现,如下所示:

public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
    this.model = model;
}

然后在onActivityResult中这样做:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 4) {
        listModel.clear();
        listModel.addAll(repository.consDataBase(context));
        recyclerViewAdapter.notifyDataSetChanged();
    }
}

答案 2 :(得分:0)

简单地说: RecyclerViewAdapter.notyfyDataSetChanged();