Android - 返回活动后无法清除信息

时间:2017-06-25 19:43:53

标签: java android database android-recyclerview recycler-adapter

点击列表并在EditText上设置信息& ImageView更新数据库信息 当我返回上一个并再次返回相同的活动时,EditText& ImageView将不会被删除,但仍会显示。 我该如何解决这个问题?

代码java类管理:

        MyDB db = new MyDB(getApplicationContext());
    List<InfoData> data = db.fetchmaindata();
    db.close();
    adapter = new MyListAdapter(getApplicationContext(), postdata(data));

    BtnLoadAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));


        }
    });
}

private View.OnClickListener onEditEmployeeSubmit = new View.OnClickListener() {

    @Override
    public void onClick(View view) {



        if (EdtNAME.length()>0 && EdtFAMILY.length()>0 && EdtDESCRIPTION.length()>0) {
            try {
                myDB.open();
                myDB.editEmployee(
                        Integer.parseInt(EdtID.getText().toString()),
                        EdtNAME.getText().toString(),
                        EdtFAMILY.getText().toString(),
                        EdtDESCRIPTION.getText().toString(),
                        ImgViewOrgsToByte(ImgViewOrgs)
                );

                //refreshList();

                Toast.makeText(getApplicationContext(), "Information was updated!", Toast.LENGTH_SHORT).show();
                EdtID.setText("");
                EdtNAME.setText("");
                EdtFAMILY.setText("");
                EdtDESCRIPTION.setText("");
                ImgViewOrgs.setImageResource(R.mipmap.ic_launcher);
                myDB.close();

            }catch (Exception e){
                e.printStackTrace();
                myDB.close();
            }
        }else{
            Toast.makeText(getApplicationContext(),"Fill in all the information!",Toast.LENGTH_SHORT).show();
        }
        //btnCancel.performClick();
    }
};

public void info(){
    findViewById(R.id.EdtID).setVisibility(View.VISIBLE);
    byte[] infoimage = Manage.image;
    if (infoimage != null && infoimage.length > 0) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(infoimage, 0, infoimage.length);
        EdtID.setText(Integer.toString(id));
        EdtNAME.setText(title);
        EdtFAMILY.setText(family);
        EdtDESCRIPTION.setText(description);
        ImgViewOrgs.setImageBitmap(bitmap);
    }else{
        return;
    }
}

private List<Info> postdata(List<InfoData> db) {
    List<Info> data = new ArrayList<>();
    for (int i = 0; i < db.size(); i++) {

        Info cur = new Info();
        cur.name = db.get(i).getName();
        cur.family = db.get(i).getFamily();
        cur.description = db.get(i).getDescription();
        cur.image = db.get(i).getImage();
        data.add(cur);

    }

    return data;
}

代码适配器:

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ViewHolder> {
private Context context;
private List<Info> data;
private LayoutInflater inflater;

public MyListAdapter(Context context, List<Info> data) {
    inflater = LayoutInflater.from(context);
    this.data = data;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.custom_row_main, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);


    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Info cur = data.get(position);
    holder.textView.setText(cur.name);
    holder.textViewf.setText(cur.family);

    byte[] infoimage = cur.image;
    Bitmap bitmap = BitmapFactory.decodeByteArray(infoimage, 0, infoimage.length);
    holder.imageView.setImageBitmap(bitmap);

    Typeface font = Typeface.createFromAsset(context.getAssets(), "BKoodkBd.ttf");
    holder.textView.setTypeface(font);
    holder.textViewf.setTypeface(font);

}

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

class ViewHolder extends RecyclerView.ViewHolder {
    private TextView textView,textViewf,textViewi;
    private ImageView imageView;    

    public ViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView.findViewById(R.id.title_main);
        textViewi = (TextView) itemView.findViewById(R.id.textViewi);
        textViewf = (TextView) itemView.findViewById(R.id.title_mainf);
        imageView = (ImageView) itemView.findViewById(R.id.imageView);
        Button btnEdit = (Button) itemView.findViewById(R.id.btn_edit);
        final Button btnDelete = (Button) itemView.findViewById(R.id.btn_delete);
        //imageView = (ImageView) itemView.findViewById(R.id.iii);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Info curr = new Info();
                curr = data.get(getPosition());
                Manage.id = curr.id;
                Manage.title = curr.name;
                Manage.family = curr.family;
                Manage.description = curr.description;
                Manage.image = curr.image;
                Manage.id = getPosition() + 1;

                Intent intent = new Intent(context, Manage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });


    }

}
}

该计划的图片

enter image description here

0 个答案:

没有答案