从片段向数据库中插入值时ListView不更新

Posted

技术标签:

【中文标题】从片段向数据库中插入值时ListView不更新【英文标题】:ListView Not updating when inserting value in database from fragment 【发布时间】:2020-02-10 07:19:15 【问题描述】:

我使用 BottomSHeetDialogfragment 创建了一个底页,按下保存按钮后,我的底页上有 2 个文本框和 1 个名为保存按钮的按钮成功到数据库,但我的列表视图没有自动更新插入的值

现在如何自动更新我的列表视图?

我的适配器类是...

public class MainListViewAdapter extends BaseAdapter 

    Context context;
    ArrayList<String> taskList;
    ArrayList<String> detailList;

    LayoutInflater layoutInflater;

    public MainListViewAdapter(Context context, ArrayList<String> taskList, ArrayList<String> detailList) 
        this.context = context;
        this.taskList = taskList;
        this.detailList = detailList;
    

    @Override
    public int getCount() 
        return taskList.size();
    

    @Override
    public Object getItem(int position) 
        return null;
    

    @Override
    public long getItemId(int position) 
        return 0;
    

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) 

        if (convertView == null) 
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.main_activity_list_view, parent, false);
        

        TextView tasktitleTv = convertView.findViewById(R.id.list_item_title_tv_id);
        TextView taskDetailsTv = convertView.findViewById(R.id.list_item_detail_tv_id);
        final CheckBox checkBox = convertView.findViewById(R.id.list_item_checkbox_id);

        convertView.setClickable(true);
        convertView.setFocusable(true);
        convertView.setBackgroundResource(android.R.drawable.menuitem_background);


        convertView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show();
            
        );

        tasktitleTv.setText(taskList.get(position));
        taskDetailsTv.setText(detailList.get(position));


        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                if (isChecked) 
                    Toast.makeText(context, "Checked", Toast.LENGTH_SHORT).show();
                    taskList.remove(position);
                    detailList.remove(position);
                    MainListViewAdapter.this.notifyDataSetChanged();
                 else 
                    return;
                
                if (isChecked) 
                    buttonView.toggle();
                
            
        );

        return convertView;
    

而我的 mainActivity.java 类是

public class MainActivity extends AppCompatActivity 

    private TextView taskHeaderTv;
    public ListView mainListview;
    private MainListViewAdapter mainListViewAdapter;

    private FloatingActionButton floatingActionButton;
    private BottomAppBar bottomAppBar;

    private DataBaseHelper dataBaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        taskHeaderTv = findViewById(R.id.task_header_tv_id);
        floatingActionButton = findViewById(R.id.floating_action_button);
        bottomAppBar = findViewById(R.id.bottomAppBar);

        fabClick();
        bottomAppBarClick();

        // set list view content
        dataBaseHelper = new DataBaseHelper(this);
        ArrayList<String> taskList = dataBaseHelper.getAllTitle();
        ArrayList<String> detailList = dataBaseHelper.getALlDetails();

        mainListview = findViewById(R.id.main_activity_listview_id);
        mainListViewAdapter = new MainListViewAdapter(this, taskList, detailList);
        mainListview.setAdapter(mainListViewAdapter);

        mainListview.setOnItemClickListener(new AdapterView.OnItemClickListener() 
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                Toast.makeText(MainActivity.this, position, Toast.LENGTH_SHORT).show();
            
        );

        mainListViewAdapter.notifyDataSetChanged();

    

我的 Fragment 类是

public class ListItemInputFragment extends BottomSheetDialogFragment 

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

        View view = inflater.inflate(R.layout.fragment_list_item_input, container, false);

        final EditText newTaskEt, detailsEt;
        TextView savBtn;
        newTaskEt = view.findViewById(R.id.new_task_et_id);
        detailsEt = view.findViewById(R.id.details_et_id);
        savBtn = view.findViewById(R.id.save_btn_id);

        savBtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                String title = newTaskEt.getText().toString();
                String detail = detailsEt.getText().toString();

                DataBaseHelper dataBaseHelper = new DataBaseHelper(getContext());
                long row = dataBaseHelper.insertData(title, detail);

                if (row == -1) 
                    Toast.makeText(getActivity().getBaseContext(), "Data not inserted", Toast.LENGTH_SHORT).show();
                 else 
                    Toast.makeText(getActivity().getBaseContext(), "Data Inserted", Toast.LENGTH_SHORT).show();
                

                dismiss();
            
        );

        return view;
    

【问题讨论】:

在片段中的 dataBaseHelper.insertData 之后显示什么列表视图? 【参考方案1】:

由于 onCreate 仅被称为 MainActivity 已创建(但不是在解除底部防护之后),您可以在 onResume 中更新您的列表,该列表在解除底部防护后调用。

主要活动

@Override
protected void onResume() 
    super.onResume();

    mainListViewAdapter.taskList = dataBaseHelper.getAllTitle();
    mainListViewAdapter.detailList = dataBaseHelper.getALlDetails();
    mainListViewAdapter.notifyDataSetChanged();

但是,我真的建议您检查 Android 架构组件和房间。 (https://developer.android.com/topic/libraries/architecture) 使用 LiveData 的 MVVM(模型 - 视图 - 视图模型)让您的生活更轻松。

【讨论】:

感谢您提供了很好的信息。是的,用另一种方式提出我的解决方案,但我认为你的解决方案比我好。非常感谢您的回复

以上是关于从片段向数据库中插入值时ListView不更新的主要内容,如果未能解决你的问题,请参考以下文章

刷新同一活动中剩余的当前片段(ListView 数据)

android - 在更改 EditText 值时无法更新列表视图

从片段更新ListView适配器内的TextView

在常规(支持)片段中使用 AsyncTaskLoader 更新适配器不更新 ListView

片段事务分离和附加后ListView不工作?

应该插入还是更新新数据?当我尝试在表行中插入空值时,为啥会死锁?