将项目从其他活动添加到列表视图 [英] Add items to listview from other activity

查看:74
本文介绍了将项目从其他活动添加到列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

第一个mainactivity启动,用户从菜单选项中使用intent启动第二个活动,然后他在其中添加一些文本到edittext中,并使用intent将其添加到第一个活动中,并将该值添加到listview中.

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:

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

}

第二活动:

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

          } 

        }

现在,我的问题是我可以将数据添加到列表视图中,但是每次回到mainactivity时,先前添加的数据都会丢失,并用我的新数据更新旧数据.

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.

我已经搜索了很多SO答案,其中大多数建议添加已经尝试过的adapter.notifyDataSetChanged();,但没有任何效果.

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

谁能说我该如何工作?

推荐答案

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

链接到Android文档

即使 startActivityForResult()是恕我直言,解决问题的最佳方法,我也会向您提示为什么它不显示旧"数据. 进行第二个练习时,您没有完成第一个练习.您也可以只完成第二项活动,您的MainActivity就会遇到

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

onResume()

因此请检查 Android生命周期

这篇关于将项目从其他活动添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆