This is a question when you dynamically add a list view item from another activity.

Asked 2 years ago, Updated 2 years ago, 84 views

Place the listview and add item buttons in Main Activity.

Press the button to go to B_Activity.

In B_Activity, I want to read the view values when I click the finish button and add a list view item located in Main Activity.

(I'll just write B and Main from here on out.))

When you connect listview with findViewById and adapter (listview.setAdapter) in B, listview appears with null point extensions. ㅠ<

android listview

2022-09-22 20:44

2 Answers

Are you saying that you approached the listView of the main activity through the findViewById() function in activity B? It's not accessible that way. When you click the Finish button in B, you must forward the selected items, that is, data, to the main activity. Read the following link.

Briefly describe the process, when you run activity B in the main activity, you run it with startActivityForResult(), and when you click the Finish button, B calls setResult(RESULT_OK,int) and finish() with data in Intent. In the main activity, you can receive the results of activity B via the onActivityResult() function. You can add the data received as onActivityResult() to listView and update it.


2022-09-22 20:44

Attached is the method code that Hanlonironi mentioned.

[MainActivity.class]

public static List<String> list;
private ArrayAdapter simpleAdapter;
private ListView listView;

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

    list = new ArrayList<>();
    list.add ("using the static variable in MainActivity");
    list.add ("Use the ActivityForResult() method");

    listView = (ListView)findViewById(R.id.list);
     simpleAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
    listView.setAdapter(simpleAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    Intent intent1 = new Intent(MainActivity.this,B_Activity.class);
                    intent1.putExtra("request",0);
                    startActivity(intent1);
                    break;
                case 1:
                    Intent intent2 = new Intent(MainActivity.this,B_Activity.class);
                    intent2.putExtra("request",1);
                    startActivityForResult(intent2,0);
                    break;
                default:
                    break;
            }

        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    simpleAdapter.notifyDataSetChanged();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String response = data.getStringExtra("data");
    list.add(response);
    simpleAdapter.notifyDataSetChanged();
}

[B_Activity.class]

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

    int request = getIntent().getIntExtra("request", -1);
    Button button = (Button) findViewById(R.id.button);
    switch (request) {
        case 0:

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MainActivity.list.add ("AAAA"); //Add data to list in the static memory variable in MainActivity
                    At the end of the finish(); // activity, the onResume method in MainActivity is executed to update the list using the dataSetChange method.
                }
            });

            break;
        case 1:

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("data", "BBBBBB");
                    setResult(0, intent);
                    At the end of the finish(); // activity, the onResume method in MainActivity is executed to update the list using the dataSetChange method.
                }
            });

            break;
    }
}


2022-09-22 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.