简体   繁体   中英

ListView is updating only one view item when I get the results from another activity, how do I get all the past results?

ListView is updating only one view item when I get the results from another activity, how do I get all the past results the user inputted and display it in the ListView?

I am using intents to send data between the two activities.

The idea is that the user lists all countries he/she has ever visited. The display might look something like this:

1964 Sweden

1970 Finland

1979 Denmark

1980 England

1982 Norway

...

2009 Ukraina

The user adds a new country by pressing Menu → Add Country. This action presents a new page (Activity) where the user can add a new entry (Country + Year).

I have been stuck for 8 hours now, so any help would come handy. Thanks!

MyCountries.java

import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
 * Created by root on 2016-09-03.
 */
public class MyCountries extends Activity {

    private ListView listView;
    private MyAdapter adapter;
    private Activity main_activity;
    String year="";
    String country="";
    ArrayList<String> info = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.action_bar);

        listView = (ListView)findViewById(R.id.listView);
        adapter = new MyAdapter(this,R.layout.list_item, info);

        main_activity = this;  // Simplifies Intent handling
    }

    @Override
    protected void onStop() {
        super.onStop();

        //listView.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onRestart() {
        super.onRestart();  // Always call the superclass method first

        // Activity being restarted from stopped state
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // An XML based menu specification.
        // See res/menu/action_menu.xml for details
        getMenuInflater().inflate(R.menu.action_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_integer_menu:

                /* Start new Activity that returns a result */
                Intent intent = new Intent(main_activity,Read_Country.class);
                main_activity.startActivityForResult(intent,0);

                return true;

            case android.R.id.home:  // Predefined icon ID

                // app icon in action bar clicked ==>  go home
                Intent intent2 = new Intent(this, MainList.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent2);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

        /*
         * Adapter to populate list with colors and integers
         */
    class MyAdapter extends ArrayAdapter<Object> {

        public MyAdapter(Context context, int textViewResourceId, List list) {
            super(context,textViewResourceId, list);
        }

        @Override   // Called when updating the ListView
        public View getView(int position, View convertView, ViewGroup parent) {
            /* Reuse super handling ==> A TextView from R.layout.list_item */
            TextView tv = (TextView) super.getView(position,convertView,parent);

                /* Find corresponding entry */
                Object obj = getItem(position);

            /* Update TextView with string information */
                tv.setText(obj.toString());
                tv.setBackgroundColor(0xffffffff);
                tv.setTextColor(0xff000000);  // Black

            return tv;
        }
    }

    /** Called when the activity receives a results. */
    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            country = result.getStringExtra("result1");
            year = result.getStringExtra("result2");
          //this is where I update the arrayList, which is an element of the arrayadapter
            info.add(year + " " + country);

/*            for (int i = 0; i < info.size(); i++) {
                System.out.println(info.get(i));
            }
            //adapter.notifyDataSetChanged();
          adapter.add(year + " " + country);  // Add new string to data list
           */
            listView.setAdapter(adapter);
        }
    }
}

Reader_Country.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by root on 2016-09-03.
 */
public class Read_Country extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.read_country);

        /* Assign listener to button */
        Button button = (Button)findViewById(R.id.done_button);
        button.setOnClickListener(new ButtonClick());
    }

    private class ButtonClick implements View.OnClickListener {
        public void onClick(View v) {
            EditText reader = (EditText)findViewById(R.id.country_reader);
            String country = reader.getText().toString();

            EditText reader2 = (EditText)findViewById(R.id.year_reader);
            String year = reader2.getText().toString();

            /* Create new result intent */
            Intent reply = new Intent();
            reply.putExtra("result1", country);
            reply.putExtra("result2", year);
            setResult(RESULT_OK,reply);
            finish();   // Close this activity
        }
    }
}

The best solution to your problem that I think would be to create a local database on the user's phone itself using SQLDatabase.

However if you wish that if the user can view these countries that he has enlisted in your app in whichever phone he logs on to,then I would suggest you use some could database , and in my opinion FireBase is a real good option.

Hope this helped you :)

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