如何从其他活动向列表视图添加项目(android studio 2.1.2)

时间:2016-07-16 17:33:30

标签: listview android-studio

我尝试搜索github,然后在这里搜索如何将项目添加到其他活动的列表视图中。

我目前正致力于我的学校项目,我需要做的是:

从其他活动中手动将项目添加到列表视图中(称为屏幕"编辑屏幕")

我的问题是,当我添加电影时,它将始终覆盖我之前在列表视图中输入的电影

公共类MainActivity扩展了AppCompatActivity {

Button btnChooseScreen ;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //reading the sharedpref to popluate the listview
    SharedPreferences save = getSharedPreferences("movie info" , MODE_PRIVATE);
    final String movieName = save.getString("MOVIE NAME" , "defualt");
    final String movieSummery = save.getString("MOVIE SUMMERY" , "defualt");
    final String imageURL = save.getString("IMAGE URL" , "defualt");
    final ArrayList<String> arrayOfMovies = new ArrayList<String>();
    //arrayOfMovies.add(movieName);


    btnChooseScreen= (Button) findViewById(R.id.btnChooseScreen);
    btnChooseScreen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //make an alert dialog to ask the user which screen would he like to go
            final AlertDialog.Builder adChooseScreen = new AlertDialog.Builder(MainActivity.this) ;
            adChooseScreen.setTitle("WHAT TO DO?");
            adChooseScreen.setPositiveButton("ADD MANUALLY", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intentToManualScreen = new Intent(MainActivity.this,EditScreen.class);
                    startActivity(intentToManualScreen);
                }
            });
            adChooseScreen.setNeutralButton("ADD FROM THE INTERNET", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Intent intentInternetScreen = new Intent(MainActivity.this , OnlineSearchScreen.class);
                    startActivity(intentInternetScreen);
                }
            });
            adChooseScreen.show();
        }
    });

    //will call the listView widget
    final ListView myMovieListView = (ListView) findViewById(R.id.listView);
    //to set the arrayList into the listView I need the component Adapter
    final ArrayAdapter<String> myMoiveArrayAdapter = new ArrayAdapter<String>(MainActivity.this , android.R.layout.simple_list_item_1 ,arrayOfMovies );
    myMovieListView.setAdapter(myMoiveArrayAdapter);
    myMovieListView.setClickable(true);
    myMoiveArrayAdapter.add(movieName);
    myMoiveArrayAdapter.notifyDataSetChanged();
    myMovieListView.setOnItemClickListener(new AdapterView.OnItemClickListener()        {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Intent intentFromListViewToEditScreen = new Intent(MainActivity.this, EditScreen.class);
            intentFromListViewToEditScreen.putExtra("movieName" ,movieName);
            intentFromListViewToEditScreen.putExtra("movieSummary", movieSummery);
            intentFromListViewToEditScreen.putExtra("imageUrl",imageURL);
            startActivity(intentFromListViewToEditScreen);


        }
    });

}
//calling the menu and the menu inflater
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu , menu);
    return super.onCreateOptionsMenu(menu);
}
//set what happens when cliking an item in the menu
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.settings :
            //build an alert dialog for the option menu
            final AlertDialog.Builder adOptionMenue = new AlertDialog.Builder(MainActivity.this) ;
            //build an alert dialog for confirmation of deleting the list
            final AlertDialog.Builder adbmakesure = new AlertDialog.Builder(MainActivity.this) ;
            adOptionMenue.setTitle("SETTINGS") ;
            //delete button
            adOptionMenue.setPositiveButton("DELETE ALL ITEMS?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                //making sure he didnt pressed delete by mistake
                adbmakesure.setTitle("ARE YOU SURE YOU WANT TO DELETE ALL ITEMS?");
                    adbmakesure.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ///////////////////////////////////
                            /////////WRITE DELETE CODE/////////
                            ///////////////////////////////////
                            ///////////////////////////////////
                        }
                    });

                adbmakesure.setNeutralButton("NO TAKE ME BACK!", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                adbmakesure.show();
                }
            }) ;
            //EXIT BUTTON
            adOptionMenue.setNeutralButton("EXIT", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //making sure he didnt pressed exit by mistake
                    adbmakesure.setTitle("CLOSE APPLICATION?");
                    adbmakesure.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            System.exit(0);
                        }
                    });
                    adbmakesure.setNeutralButton("NO I WANT TO SEE MORE!", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    });
                    adbmakesure.show();
                }
            });
            adOptionMenue.show();
            return true ;
    }
    return super.onOptionsItemSelected(item);

这就是编辑屏幕

公共类EditScreen扩展了AppCompatActivity {

EditText etMovieName ;
EditText etMoviewSummery;
EditText etURL ;

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


    etMovieName = (EditText) findViewById(R.id.etMovieName);
   // String inputMovieName = etMovieName.getText().toString();
    etMoviewSummery = (EditText) findViewById(R.id.etSummery);
    //String inputMovieSummery = etMoviewSummery.getText().toString();
    etURL = (EditText) findViewById(R.id.etURL);
    //String inputURL = etURL.getText().toString();
    Button btnsave = (Button) findViewById(R.id.btnsave);
    btnsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (etMovieName.getText().toString().isEmpty() || etMoviewSummery.getText().toString().isEmpty()
                    || etURL.getText().toString().isEmpty()) {
                Toast.makeText(EditScreen.this, "Must write details!!!", Toast.LENGTH_SHORT).show();
            }
            else {
                Savedetails();
                Intent intentgoback = new Intent(EditScreen.this, MainActivity.class);
                startActivity(intentgoback);
            }
        }
    });

    //when item clicked at the mainactiveryscreen the details come from here
    final String sMovieName= getIntent().getStringExtra("movieName");
    final String sMovieSummery= getIntent().getStringExtra("movieSummary");
    final String sURL= getIntent().getStringExtra("imageUrl");
    if (sMovieName!=null && sMovieSummery !=null && sURL!=null ) {
        etMovieName.setText(sMovieName);
        etMoviewSummery.setText(sMovieSummery);
        etURL.setText(sURL);
    }
    else {

    }

}


//a method to save the info input
public void Savedetails() {
    SharedPreferences save = getSharedPreferences("movie info", MODE_PRIVATE);
    SharedPreferences.Editor edit = save.edit();
        edit.putString("MOVIE NAME", etMovieName.getText().toString());
        edit.putString("MOVIE SUMMERY", etMoviewSummery.getText().toString());
        edit.putString("IMAGE URL", etURL.getText().toString());
        edit.apply();
        Toast.makeText(EditScreen.this, "DETAILS SAVED!", Toast.LENGTH_SHORT).show();


}

}

感谢您的帮助,祝您有个美好的一天:D

1 个答案:

答案 0 :(得分:1)

您始终只在列表中放置一个项目 - 在onCreate中。永远不会添加第二个项目。 2个理由:

  1. 您在列表中设置了一个覆盖现有适配器的新适配器

  2. 当您返回时,您正在创建新活动。这是一个完整的新的,不包含任何内容,没有存储的列表项。它与您第一次启动活动时的状态基本相同。

  3. 我不会为你解决所有问题,因为你必须自己学习(这是一个学校项目;)),但我可以给你一些提示:

    • 将主Activity设置为mainfest中的编辑活动的父活动(许多活动......)
    • 通过startActivityForResult()启动编辑活动并覆盖onActivityResult以侦听例如新州
    • 在EditActivity调用finish()中,而不是启动新的主Activity
    • 在调用finish()之前,使用结果代码(RESULT_OK或RESULT_CANCELLED)调用setResult(),并使用包含主Activity的信息的Intent
    • 在主要活动中使用导致onActivityResult将新项目添加到适配器