ListView not update after insert new Data

Viewed 56

This is my Main Activity where I call showData funtion and pass to the adapter

public class MainActivity extends AppCompatActivity {
    Button add,add1;
    ListView list;
    Database db;
    ArrayList<studentmodel>data;
    EditText roll, name, fname, student_class;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new Database(this);
        add = findViewById(R.id.btnAdd);
        list=findViewById(R.id.list_item);
        data=new ArrayList<>();
        data=db.showData();
        CustomAdapter ad=new CustomAdapter(this,data);
        list.setAdapter(ad);
        data=db.showData();
        ad.notifyDataSetChanged();
    }
}

This is my Custom Adapter

public class CustomAdapter extends ArrayAdapter {
    ArrayList<studentmodel> student;
    Context context;

    public CustomAdapter(@NonNull Context context, ArrayList<studentmodel> student) {
        super(context, R.layout.customlayout, student);
        this.context = context;
        this.student = student;

    }

    public studentmodel getItem(int position) {
        return student.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public View getView(int position, View i, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.customlayout, parent, false);
        studentmodel s1 = student.get(position);
       TextView adresult=view.findViewById(R.id.txtAddResult);
       TextView delete=view.findViewById(R.id.txtDeleteResult);
       TextView name=view.findViewById(R.id.txtName);
       TextView s_class=view.findViewById(R.id.txts_class);
       TextView roll=view.findViewById(R.id.txtRoll);
       name.setText(s1.getStudent_name());
       s_class.setText(s1.getStudent_class());
       roll.setText(String.valueOf(s1.getStudent_roll()));
       adresult.setOnClickListener(v -> {
           Intent intent=new Intent(context,AddResult.class);
           context.startActivity(intent);
       });
        return view;
    }
}

This is my dialog where I add student information

AlertDialog.Builder builder=new AlertDialog.Builder(this);

        View view=LayoutInflater.from(this).inflate(R.layout.customdialog,null);
        builder.setView(view);
        name=view.findViewById(R.id.edtName);
        roll=view.findViewById(R.id.edtRoll);
        student_class=view.findViewById(R.id.EdtClassName);
        add1=view.findViewById(R.id.btnadd);
        add1.setOnClickListener(v1 -> {
            String sname=name.getText().toString();
            String sturoll=roll.getText().toString();
            String sclass=student_class.getText().toString();
            if(sname.equals("")||roll.equals("")||sclass.equals(""))
            {
                Toast.makeText(MainActivity.this, "Please fill all fields", Toast.LENGTH_SHORT).show();
            }
            else {
                int sroll = Integer.parseInt(sturoll);
                studentmodel student = new studentmodel(sroll, sname, sclass);
                int i = db.studentInsertion(student);
                if (i == 1) {
                    Toast.makeText(MainActivity.this, "Data is inserted", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Data is not inserted", Toast.LENGTH_SHORT).show();
                }
            }
        });builder.show();
    }

}

When I open my application first time Data of a student is show but when I insert new data in database with the help of dialog then this Data is not show in ListView but when I finish my application and reopen then I see information of a student show which I entered and old data. I want to see my Data when I enter without not closing my app

1 Answers

Important note: It is not the best practice, fetching database content from the Main thread synchronously. It may causes UI blocking issue. Use any background thread to perform this task. Like AsyncTask, Coroutines (If Kotlin)

At first on your onCreate method in Main Activity, remove unnecessary codes(Commented out).

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = new Database(this);
    add = findViewById(R.id.btnAdd);
    list=findViewById(R.id.list_item);
    data = new ArrayList<>();
    data = db.showData();
    CustomAdapter ad=new CustomAdapter(this, data);
    list.setAdapter(ad);
    // data=db.showData();
    // ad.notifyDataSetChanged();
}

Now you have to pass CustomAdapter instance "ad", inside your AlertDialog class. From there you can notify that data set changed by calling ad.notifyDataSetChanged() method.

//...
studentmodel student = new studentmodel(sroll, sname, sclass);
int i = db.studentInsertion(student);
ad.notifyDataSetChanged();
//...

Hopefully you can overcome your problem. If not let me know.

Related