简体   繁体   English

ListView 不刷新数据库中的内容

[英]ListView not refreshing contents from database

I'm trying to make a simple list with the ability to add new items to it, delete and modify them.我正在尝试制作一个简单的列表,能够向其中添加新项目、删除和修改它们。 The contents of the list should be saved in SQLite.列表的内容应该保存在 SQLite 中。 My problem is the following: When I add a new item, the list isn't refreshing, and the item appears only after I exited the app, and open it again.I found a workaround, but it's hacky and doesn't entirely work.我的问题如下:当我添加一个新项目时,列表没有刷新,只有在我退出应用程序并再次打开它后才会出现该项目。 .

public class MainActivity extends AppCompatActivity {
    SaveClass save = new SaveClass(this);
    private ListView mShoppingList;
    private EditText mItemEdit;
    private Button mAddButton;
    private ArrayAdapterElem mAdapter;
    List<ItemList> elemek;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        elemek = save.getAllItems();

        mAdapter = new ArrayAdapterElem(this, elemek);
        mShoppingList = findViewById(R.id.shopping_listView);
        mItemEdit = (EditText) findViewById(R.id.item_editText);
        mAddButton = (Button) findViewById(R.id.add_button);
        mShoppingList.setAdapter(mAdapter);


        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemList item = new ItemList(mItemEdit.getText().toString());
                mItemEdit.setText("");
                save.addElem(item);
                mAdapter.add(item);
                mAdapter.notifyDataSetChanged();
            }
        });

And this is my custom ArrayAdapter class:这是我的自定义 ArrayAdapter 类:

public class ArrayAdapterElem extends ArrayAdapter<ItemList> {

    private int elem;

    public ArrayAdapterElem(Context context, List<ItemList> elemek) {
        super(context, 0, elemek);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemList elem = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.lista_elem, parent, false);
        }
        TextView elemnev = (TextView) convertView.findViewById(R.id.Elem);

        if (elem.checked == true) {
            elemnev = (TextView) convertView.findViewById(R.id.Elem);
            elemnev.setTextColor(Color.GRAY);
            elemnev.setPaintFlags(elemnev.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }
        else
        {
            elemnev.setTextColor(Color.BLACK);
            elemnev.setPaintFlags(elemnev.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
        }

        elemnev.setText(elem.listaElem);

        return convertView;
    }

}

Right now I save the item in SQLite and add the item to ListView manually.现在我将项目保存在 SQLite 中并手动将项目添加到 ListView。 I know it means the new object in the database and the object in the list are not the same, hence my modifications doesn't work on the saved item, until I reload the app to get the object from the database.我知道这意味着数据库中的新对象和列表中的对象不同,因此我的修改不适用于保存的项目,直到我重新加载应用程序以从数据库中获取对象。 So I'd like to find a better solution for this.所以我想为此找到一个更好的解决方案。 If I just simply save the item in the database, and call notifyDataSetChanged() method on the adapter, it doesn't refresh items in the list from the database.如果我只是简单地将项目保存在数据库中,并在适配器上调用 notifyDataSetChanged() 方法,它不会从数据库刷新列表中的项目。 Should I implement notifyDataSetChanged in my custom ArrayAdapter class, or is there some much simpler solution I fail to see?我应该在我的自定义 ArrayAdapter 类中实现 notifyDataSetChanged ,还是有一些我看不到的更简单的解决方案?

Thank you for any help in adavance.感谢您提供的任何帮助。

I think you are not updating the attached data list while saving the data into SaveClass object.我认为您在将数据保存到 SaveClass 对象时没有更新附加的数据列表。 Try the below it may work if my understanding is correct.如果我的理解是正确的,请尝试以下操作。

Edit your mAddButton click listener as mentioned below.如下所述编辑您的 mAddButton 单击侦听器。

mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemList item = new ItemList(mItemEdit.getText().toString());
                mItemEdit.setText("");
                save.addElem(item);
                mAdapter.add(item); // this line might not required.
                elemek.add(item);
                mAdapter.notifyDataSetChanged();
            }
        });

You can achieve the same like this您可以像这样实现相同的目标
make a method in your adapter class like this在你的适配器类中创建一个这样的方法

public class ArrayAdapterElem extends ArrayAdapter<ItemList> {
    private int elem;
   private List<ItemList>arraylist;

    public ArrayAdapterElem(Context context, List<ItemList> elemek) {
        super(context, 0, elemek);
    }
    public void updateList(List<ItemList> list){
         this.arraylist = list;
         notifyDataSetChanged();
       }

in your onclick listener在您的 onclick 侦听器中

mAddButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ItemList item = new ItemList(mItemEdit.getText().toString());
            mItemEdit.setText("");
            save.addElem(item);

            adapter.updateList(save.getAllItems());

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM