我正在从 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;
} }

主事件
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);
    }
} }

添加组
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);
} }

最佳答案

为了让您的代码正常工作,您需要按如下方式更改 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();
    }
}

关于android-recyclerview - 添加数据后 RecyclerView 不更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35285375/

10-11 19:28