Update list view after navigating back to previous activity

问题: Excuse my noobness. I just don't understand how to implement it to work with my code. What I'm doing is editing a name that's in a list view. When editing the name in "Edit...

问题:

Excuse my noobness. I just don't understand how to implement it to work with my code. What I'm doing is editing a name that's in a list view. When editing the name in "EditDeleteList" and get back to the previous activity (ListView) to see the name updated within the list view, nothing happens. I have to go out of the activity completely to see the change reflected. How do I get this to update? I implement an onActivityReult() method but then get this error message "java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference" so I removed it completely. How I could get the list view to update without getting that error message?

ListView.Java

    public class ListView extends AppCompatActivity {
    private static final String TAG = "ListView";
    DatabaseHelper mDatabaseHelper;
    Button btnAdd;
    private EditText editText;
    private android.widget.ListView listView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        mDatabaseHelper = new DatabaseHelper(this);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        editText = (EditText) findViewById(R.id.editText);
        listView = (android.widget.ListView) findViewById(R.id.lv);

        ArrayList<String> list = getIntent().getStringArrayListExtra("myList");

        android.widget.ListView lv = (android.widget.ListView) findViewById(R.id.lv);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        lv.setAdapter(adapter);


        //Takes user back to the main activity
        ImageView ivBack = (ImageView) findViewById(R.id.ivBackArrow);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: pressed back arrow");
                Intent intent = new Intent(ListView.this, MainActivity.class);
                startActivity(intent);
            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newEntry = editText.getText().toString();

                if (editText.length() != 0) {
                    addData(newEntry);
                    editText.setText("");
                } else {
                    toastMessage("you must put something in the text field");
                }
            }
        });

        populateListView();
    }

    public void addData(String newEntry) {
        boolean insertData = mDatabaseHelper.addData(newEntry);

        if (insertData) {
            toastMessage("Successfully inserted");
            recreate();
        } else {
            toastMessage("Whoops, something went wrong");
        }
    }

    private void toastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    private void populateListView() {
        Log.d(TAG, "populateListView: displaying data in the listview");

        //get data and append to list
        Cursor data = mDatabaseHelper.getData();
        ArrayList<String> listData = new ArrayList<>();
        while(data.moveToNext()) {
            //get the value from the database in column 1
            //set it to the arraylist
            listData.add(data.getString(1));
        }
        //create arraylist and set it to the adapter
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        listView.setAdapter(adapter);

        //set onclick listen to edit activity
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String name = adapterView.getItemAtPosition(position).toString();
                Log.d(TAG, "onItemClick: you clicked on " + name);

                Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
                int itemID = -1;
                while (data.moveToNext()) {
                    itemID = data.getInt(0);
                }
                if (itemID > -1) {
                    Log.d(TAG, "onItemID: the ID is: " + itemID);
                    Intent editScreenIntent = new Intent(ListView.this, EditDeleteList.class);
                    editScreenIntent.putExtra("id",itemID);
                    editScreenIntent.putExtra("name",name);
                    startActivity(editScreenIntent);
                } else {
                    toastMessage("No ID found");
                }
            }
        });
    }
}

EditDeleteList.java

    public class EditDeleteList extends AppCompatActivity {

    private static final String TAG = "EditDeleteList";

    DatabaseHelper mDatabaseHelper;
    private ImageView ivDelete;
    private ImageView ivApprove;
    private EditText editHashtag;
    private String selectedName;
    private int selectedID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_delete);

        mDatabaseHelper = new DatabaseHelper(this);
        editHashtag = (EditText) findViewById(R.id.editHashtag);
        ivDelete = (ImageView) findViewById(R.id.ivDelete);
        ivApprove = (ImageView) findViewById(R.id.ivApprove);

        //get the intent extra from the ListView activity
        final Intent receivedIntent = getIntent();

        //get item ID passed as an extra
        selectedID = receivedIntent.getIntExtra("id", -1);

        //get name passed as an extra
        selectedName = receivedIntent.getStringExtra("name");

        //set text field to selected item text
        editHashtag.setText(selectedName);

        ivApprove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item = editHashtag.getText().toString();
                if (!item.equals(null)) {
                    mDatabaseHelper.updateName(item, selectedID, selectedName);
                } else {
                    toastMessage("you must enter a #hashtag");
                }
                finish();
            }
        });

    }

    private void toastMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

In the EditDeleteList.java I have an onClickListener that saves the changes and goes back to the previous activity by using finish();

ivApprove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String item = editHashtag.getText().toString();
            if (!item.equals(null)) {
                mDatabaseHelper.updateName(item, selectedID, selectedName);
            } else {
                toastMessage("you must enter a #hashtag");
            }
            finish();
        }
    });

回答1:

Notify the adapter in some part of the activity lifecycle.

OnCreate() should not run when you go back (this is the reason you have to completely recreate the activity to see the list updated) so you should use OnRestart/OnStart/OnResume to notify the adapter to check for new items.

Check this image

  • 发表于 2019-03-08 00:02
  • 阅读 ( 304 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除