【问题标题】:ListView not update after insert new Data插入新数据后 ListView 不更新
【发布时间】:2021-11-23 14:00:22
【问题描述】:

这是我调用 showData 函数并传递给的主要活动 适配器

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

这是我的自定义适配器

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

这是我添加学生信息的对话框

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

}

当我第一次打开我的应用程序时,会显示学生的数据,但是 当我在对话框的帮助下在数据库中插入新数据时,此数据不会显示在 ListView 中,但是当我完成我的应用程序并重新打开时,我会看到我输入的学生节目信息和旧数据。我想在输入时查看我的数据而不关闭我的应用程序

【问题讨论】:

  • 插入完成后致电notifyDataSetChanged()
  • 我在插入后使用 notifyDataSetChanged() 但它不起作用

标签: android android-listview


【解决方案1】:

重要提示:获取数据库内容不是最佳做法 从主线程同步。它可能会导致 UI 阻塞问题。使用任何后台线程来执行此任务。像 AsyncTask、协程(如果 Kotlin)

首先在 Main Activity 中的 onCreate 方法上,删除不必要的代码(已注释掉)。

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

现在您必须在 AlertDialog 类中传递 CustomAdapter 实例“ad”。从那里您可以通过调用 ad.notifyDataSetChanged() 方法来通知数据集已更改。

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

希望你能克服你的问题。如果不告诉我。

【讨论】:

  • 我已经使用了你的方法,但我的问题没有解决我们必须最小化然后数据更新
  • @Mubashar Ali 或许我对“我们必须最小化然后数据更新”有最好的解释。其实我也不清楚。
猜你喜欢
  • 2021-06-22
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2015-12-18
  • 1970-01-01
  • 2019-04-16
  • 1970-01-01
相关资源
最近更新 更多