1

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

    }
}
Jason
  • 366
  • 2
  • 4
  • 17

1 Answers1

1

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);
NavinRaj Pandey
  • 1,674
  • 2
  • 26
  • 40
  • 1
    ok so I need to write this line of code in between? list = i.getStringArrayListExtra("country&year"); – Jason Sep 14 '14 at 15:59
  • if your list not coming from from another activity this post will be useful for you http://stackoverflow.com/questions/6087198/passing-a-list-to-another-activity-in-android – NavinRaj Pandey Sep 14 '14 at 16:15
  • 1
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); Intent i = getIntent(); adapter=lv.getAdapter();// i get an exception here it's not working :( list = i.getStringArrayListExtra("country&year"); adapter.addAll(list); adapter.notifyDataSetChanged(); – Jason Sep 14 '14 at 16:18
  • 1
    I can't convert an adapter to a list adapter – Jason Sep 14 '14 at 16:24
  • try the latest edited code and make sure you are getting data to your list from intent – NavinRaj Pandey Sep 14 '14 at 16:43
  • I did exactly the same, program is crashing. – Jason Sep 14 '14 at 16:58
  • post the logcat error message and is the data coming to list from intent? – NavinRaj Pandey Sep 14 '14 at 17:06
  • Please check the logcat error, it was to long so i had to post it as an answer! – Jason Sep 14 '14 at 17:38
  • I think the problem is I'm receiving before sending. – Jason Sep 14 '14 at 19:24