简体   繁体   中英

sending and receiving Arraylist using intent? Android

I'm trying to create a list of countries, that's to be added by the user during run time.

I'm using intent & adapter, and listview to show the countries.

When the activity start"MyCountries"the user click on the button add country, a new activity will start" Add Country", my question here is why I can't see any country on the list? i'm I sending and receiving correctly? code for MyCountries:

public class MyCountries extends Activity {



private ListView lv;
private ArrayAdapter<String> adapter;
private ArrayList<String>list;


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

    //Button compute=(Button)findViewById(R.id.cancel);

    lv=(ListView)findViewById(R.id.listView1);

    list=new ArrayList<String>();

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



    Intent i = getIntent();  

    list = i.getStringArrayListExtra("country&year");

    adapter.notifyDataSetChanged();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my_countries, menu);
    return true;
}
public void onClick(View v) {

    Intent intent = new Intent(v.getContext(), AddCountry.class);
    startActivityForResult(intent,1);
}

code for "AddCountry"

public class AddCountry extends Activity {
private EditText name;
private EditText year;
public String country_name;
public int visited_year;
public ArrayList<String>arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_country);
    name = (EditText) findViewById(R.id.country);

    year = (EditText) findViewById(R.id.year);
    arr= new ArrayList<String>();
    Button button=(Button)findViewById(R.id.add);
}



public void onClick(View v) {

    switch (v.getId()) {
    case R.id.add:
        //Toast.makeText(this, "Add was clicked", Toast.LENGTH_LONG).show();
        //new activity
        Intent sending = new Intent();

        country_name = name.getText().toString();
        visited_year = Integer.parseInt(year.getText().toString());

        for(int i=0;i<arr.size();i++){
            arr.add(country_name+""+visited_year);
            //startActivity(sending);
        }
        sending.putStringArrayListExtra("country&year", arr);

    }
}

you are sending empty list to adapter:

list=new ArrayList<String>();// empty list

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

// get data from intent(to your list) before initializing your adapter and you don't need to notify

  list = i.getStringArrayListExtra("country&year");

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

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