Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

android - Add items to listview from other activity

scenario:

First mainactivity launches and from the menu option user launches second activity using intent and there he adds some text to edittext and get that edittext value using intent to the first activity and add that value to the listview.

FirstActivity:

public class MainActivity extends Activity {

    ListView lv;
    EditText et;
    String AddedTask ;
    ArrayList<Model> modelList;
    CustomAdapter adapter;

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

              Intent intent = getIntent();
                if (intent.hasExtra("NewTask")) {
                AddedTask = this.getIntent().getExtras().getString("NewTask");
                lv = (ListView) findViewById(R.id.listViewData);
                String name = AddedTask;
                Model md = new Model(name);
                modelList.add(md);
                adapter = new CustomAdapter(getApplicationContext(), modelList);
                lv.setAdapter(adapter);
                adapter.notifyDataSetChanged();
          }



    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        case R.id.action_add_task:
            Intent i = new Intent(MainActivity.this, AddTask.class);
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

Second Activity:

public class AddTask extends Activity {
Button addtask;
      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_task);

            // get action bar   
            ActionBar actionBar = getActionBar();

            // Enabling Up / Back navigation
            actionBar.setDisplayHomeAsUpEnabled(true);

            addtask = (Button) findViewById(R.id.btnaddlist);
            findViewById(R.id.btnaddlist).setOnClickListener(
                      new View.OnClickListener() {
                          public void onClick(View arg0) {
                              EditText edit = (EditText) findViewById(R.id.tskname);
                              Intent i = new Intent(AddTask.this,
                                      MainActivity.class);
                              //Bundle bundle = new Bundle();
                              String TaskName = edit.getText().toString();
                              //bundle.putString("NewTask", TaskName);
                              i.putExtra("NewTask", TaskName);
                              i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                              //i.putExtras(bundle);
                              startActivity(i);
                          }
                      });

          } 

        }

Now my problem is I'm able to add the data to the listview but each time I come back to mainactivity the previous data which was added is lost and updating the old data with my new data.

I have searched for many SO answers and most of them suggest to add adapter.notifyDataSetChanged(); which I have already tried and nothing worked.

I have done by checking the adapter is null or updating the data this way and getting null pointer exception:

if ( adapter== null )
{
   adapter = new CustomAdapter(getApplicationContext(), modelList);
  lv.setAdapter(adapter);
}

Can anyone say me how do I get this working ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
new View.OnClickListener() {
                      public void onClick(View arg0) {
                          EditText edit = (EditText) findViewById(R.id.tskname);
                          Intent i = new Intent(AddTask.this,
                                  MainActivity.class);
                          //Bundle bundle = new Bundle();
                          String TaskName = edit.getText().toString();
                          //bundle.putString("NewTask", TaskName);
                          i.putExtra("NewTask", TaskName);
                          i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                          //i.putExtras(bundle);
                          startActivity(i);
                      }
                  });

You are starting a new Activity each time you want to add an item.
Consider using

startActivityForResult()  

Link to Android Documentation

Even if startActivityForResult() is IMHO the best way to fix your problem, i'll give you a hint why it doesn't show your "old" data. You don't finish your first Activity when going to the Second. You could aswell just finish your second activity and your MainActivity would run into

onResume()

therefor check the Android lifecycle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...