0

After clicking on the list and setting the information on EditText & ImageView to update the database information When I return to the previous one and return to the same activity again, the information on EditText & ImageView will not be deleted and will still be displayed. How can I fix this problem?

code java class Manage:

        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;
}

code adapter:

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);
            }
        });


    }

}
}

Picture of the program

enter image description here

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Do you happen to do anything in `onResume()` such as set the text there? Also, your English is fine, we don't need to know you aren't non-native. As long as you try your best, we will edit when necessary ;) – codeMagic Jun 25 '17 at 19:48
  • No onResume() it's not in the code After clicking on an active list, it re-runs. Is it a problem? There is a solution? – Reza Mardani Jun 25 '17 at 19:52
  • I'm not exactly sure what you have going on. Can you explain what you mean by *"When I return to the previous one "*? Also, *"it re-runs"*...what does? – codeMagic Jun 25 '17 at 19:55
  • After clicking on the list that displays the same activity code Intent intent = new Intent(context, Manage.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); Runs And after the return to the same activity, the information is not cleared – Reza Mardani Jun 25 '17 at 19:57
  • Link Click : https://ibb.co/bTJNyQ – Reza Mardani Jun 25 '17 at 20:02
  • A couple options you may have, if I understand, is to override `onResume()` and set the 'Views' there to be blank. Or use `startActivityForResult()` and in `onActivityResult()` reset the views there. – codeMagic Jun 25 '17 at 20:05
  • I have not worked with these methods. If I use another activity to update information, is this standard? – Reza Mardani Jun 26 '17 at 10:07
  • Possibly but it depends on what you are doing. See [my answer here](https://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) on using `startActivityForResult()` – codeMagic Jun 26 '17 at 12:29
  • StartActivityForResult () Where do I put this method and how? – Reza Mardani Jun 26 '17 at 12:34
  • It would replace `startActivity()`. Read through that answer and look at [the documentation](https://developer.android.com/training/basics/intents/result.html) then give it a try. – codeMagic Jun 26 '17 at 12:36

0 Answers0