我正在使用recyclerview
和cardview
来显示详细信息,但是当我从sqlite
删除特定行时,它仍然在视图中我现在不应该在哪里编码来刷新删除后recyclerview
以及删除视图后如果我尝试添加新的详细信息,即卡片未显示在视图中。
当我滚动它时,我试图实现radiobutton
更改如何停止它。
我是Android和recyclerview
的新用户,因此我不知道在哪里实施notifyDataSetChanged()
或invalidate()
等代码
你也可以帮我解决上述问题。
MyAdapter类
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
static List<EmployeeDetails> dbList;
static Context context;
MyAdapter(Context context, List<EmployeeDetails> dbList ){
this.dbList = new ArrayList<EmployeeDetails>();
this.context = context;
this.dbList = dbList;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.card_item, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.name.setText(dbList.get(position).getName1());
holder.phone.setText(dbList.get(position).getPhone1());
}
@Override
public int getItemCount() {
return dbList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView name,phone;
public ImageView i,edit,delete;
// public RadioButton p,a,l;
DatabaseHelper helper=new DatabaseHelper(MyAdapter.context);
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
i=(ImageView)itemLayoutView.findViewById(R.id.empimage);
name = (TextView) itemLayoutView
.findViewById(R.id.rvname);
phone = (TextView)itemLayoutView.findViewById(R.id.rvphone);
edit=(ImageView)itemLayoutView.findViewById(R.id.edit);
delete=(ImageView)itemLayoutView.findViewById(R.id.delete);
/* p=(RadioButton)itemLayoutView.findViewById(R.id.Present);
a=(RadioButton)itemLayoutView.findViewById(R.id.Absent);
l=(RadioButton)itemLayoutView.findViewById(R.id.Leave);
*/
itemLayoutView.setOnClickListener(this);
delete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v)
{
String s=name.getText().toString();
EmployeeDetails e=new EmployeeDetails();
helper.deleteARow(s);
}
}
);
}
@Override
public void onClick(View v) {
}
实施Recyclerview
的课程
public class Employees extends Fragment implements View.OnClickListener {
DatabaseHelper helpher;
List<EmployeeDetails> dbList;
RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public Employees() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.employees, container, false);
FloatingActionButton fab= (FloatingActionButton) rootView.findViewById(R.id.fab);
fab.setOnClickListener(this);
//Recycle
helpher=new DatabaseHelper(getActivity());
dbList= new ArrayList<EmployeeDetails>();
dbList = helpher.getDataFromDB();
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter
mAdapter = new MyAdapter(getActivity(),dbList);
mRecyclerView.setAdapter(mAdapter);
// mAdapter.notifyDataSetChanged();
// mRecyclerView.invalidate();
return rootView;
}
@Override
public void onClick(View view) {
Intent intent=new Intent(getActivity(),addemployee.class);
startActivity(intent);
}
帮助我,我在互联网上搜索了很多,但我找不到合适的方式
修改
我的db代码检索数据
public List<EmployeeDetails> getDataFromDB(){
List<EmployeeDetails> modelList = new ArrayList<EmployeeDetails>();
String query = "select name,phone_no from "+TABLE_NAME1;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query,null);
if (cursor.moveToFirst()){
do {
EmployeeDetails model = new EmployeeDetails();
model.setName1(cursor.getString(0));
model.setPhone1(cursor.getString(1));
// model.setRoll(cursor.getString(2));
//model.setAddress(cursor.getString(3));
//model.setBranch(cursor.getString(4));
modelList.add(model);
}while (cursor.moveToNext());
}
答案 0 :(得分:0)
看起来您想在点击删除图片后删除该行。
好的,请致电notifyItemRemoved(position);
在delete.setOnClickListene(.....)。
并确保您的删除操作从dbList实例中删除该条目,否则刷新后将显示已删除的行(当您重新运行活动或滚动时)
如果函数helper.deleteARow(s);
刷新dbList,则在delete.setOnClickListene(.....)内的notifyItemRemoved(position);
行之后调用helper.deleteARow(s);
。
新代码:
delete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v)
{
String s=name.getText().toString();
EmployeeDetails e=new EmployeeDetails();
helper.deleteARow(s);
notifyItemRemoved(position);`
}
}
);
你也可以调用notifyDataSetChanged(),但它会刷新所有不必要的行。