简体   繁体   中英

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. 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:

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. 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. Should I implement notifyDataSetChanged in my custom ArrayAdapter class, or is there some much simpler solution I fail to see?

Thank you for any help in adavance.

I think you are not updating the attached data list while saving the data into SaveClass object. Try the below it may work if my understanding is correct.

Edit your mAddButton click listener as mentioned below.

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

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

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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