从另一个意图添加项目到数组列表

时间:2016-10-10 01:28:14

标签: java android listview android-intent arraylist

我有一个具有Book对象的ListView的应用程序。我单击一个按钮,将我带到另一个活动(AddBook),在那里我可以添加另一本书并将信息传递回MainActivity。我可以创建原始Book对象并将其添加到ListView,但如果我尝试通过单击添加按钮(在MainActivity上)添加第二本书并在返回MainActivity时再次通过AddBook,则我添加的第一本书将被替换由第二个条目。我想我需要一个循环或条件,但我无法弄清楚。

我在第94行添加了if语句,因为在我填写AddBook Activity上的字段之前会自动添加该对象

    package com.the93rd.android.bookcatalog;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

Button mAddBook;
public ArrayList<Book> books;
public ListView listView;
public BookListViewAdapter bookAdapter;

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

    //Array List of Book objects
    books = new ArrayList<>();

    books.add(new Book("J.K. Rolling", "Harry Potter and The Sorcerers Stone", 8,
            "0439708184", "Yes"));
    books.add(new Book("Eoin Colfer", "The Artic Accident", 5,
            "1423124529", "Yes"));
    books.add(new Book("George S. Clason", "The Richest Man in Babylon", 9,
            "1505339111", "No"));
    books.add(new Book("Khaled Hosseini", "The Kite Runner", 10,
            "159463193X", "Yes"));
    books.add(new Book("Daniel Keyes", "Flowers For Algernon", 8,
            "0156030306", "No"));

    listView = (ListView) findViewById(R.id.list);
    bookAdapter = new BookListViewAdapter(this, books);

    listView.setAdapter(bookAdapter);


    //Open BookDetails Activity to Display more book information
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //Get ArrayList element
            Book book = books.get(position);

            //Get fields for current Book element
            String passAuthor = book.getAuthor();
            String passTitle = book.getTitle();
            int passRating = book.getRating();
            String passIsbn = book.getIsbn();
            String passSeries = book.getSeries();

            Toast.makeText(MainActivity.this, book.getAuthor(), Toast.LENGTH_SHORT).show();

            //Pass Extras for Book element to BookDetails Activity
            Intent openDetail = new Intent(MainActivity.this, BookDetails.class);
            openDetail.putExtra("Author", passAuthor);
            openDetail.putExtra("Title", passTitle);
            openDetail.putExtra("Rating", passRating);
            openDetail.putExtra("Isbn", passIsbn);
            openDetail.putExtra("Series", passSeries);
            startActivity(openDetail);
        }
    });

    //Open AddBook Activity
    mAddBook = (Button) findViewById(R.id.add_book);
    mAddBook.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent addBook = new Intent(MainActivity.this, AddBook.class);
            startActivity(addBook);
        }
    });

    //Get extras from AddBook Activity
    Intent intent = getIntent();

    String addAuthor = intent.getStringExtra("addAuthor");
    String addTitle = intent.getStringExtra("addTitle");
    String addRating = intent.getStringExtra("addRating");
    String addIsbn = intent.getStringExtra("addIsbn");
    String addSeries = intent.getStringExtra("addSeries");

    if(addAuthor != null){

        //Add variables from AddBook activity to new Book object
        books.add(new Book(addAuthor, addTitle, 5, addIsbn, addSeries));
        bookAdapter.notifyDataSetChanged();
    }

}

}

2 个答案:

答案 0 :(得分:0)

您可以使用startActivityForResult而不是startActivity,因此您可以从AdBook Activity接收数据,例如

 package com.the93rd.android.bookcatalog;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {
    Button mAddBook;
    public ArrayList<Book> books;
    public ListView listView;
    public BookListViewAdapter bookAdapter;

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

        //Array List of Book objects
        books = new ArrayList<>();

        books.add(new Book("J.K. Rolling", "Harry Potter and The Sorcerers Stone", 8,
                "0439708184", "Yes"));
        books.add(new Book("Eoin Colfer", "The Artic Accident", 5,
                "1423124529", "Yes"));
        books.add(new Book("George S. Clason", "The Richest Man in Babylon", 9,
                "1505339111", "No"));
        books.add(new Book("Khaled Hosseini", "The Kite Runner", 10,
                "159463193X", "Yes"));
        books.add(new Book("Daniel Keyes", "Flowers For Algernon", 8,
                "0156030306", "No"));

        listView = (ListView) findViewById(R.id.list);
        bookAdapter = new BookListViewAdapter(this, books);

        listView.setAdapter(bookAdapter);


        //Open BookDetails Activity to Display more book information
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //Get ArrayList element
                Book book = books.get(position);

                //Get fields for current Book element
                String passAuthor = book.getAuthor();
                String passTitle = book.getTitle();
                int passRating = book.getRating();
                String passIsbn = book.getIsbn();
                String passSeries = book.getSeries();

                Toast.makeText(MainActivity.this, book.getAuthor(), Toast.LENGTH_SHORT).show();

                //Pass Extras for Book element to BookDetails Activity
                Intent openDetail = new Intent(MainActivity.this, BookDetails.class);
                openDetail.putExtra("Author", passAuthor);
                openDetail.putExtra("Title", passTitle);
                openDetail.putExtra("Rating", passRating);
                openDetail.putExtra("Isbn", passIsbn);
                openDetail.putExtra("Series", passSeries);
                startActivity(openDetail);
            }
        });

        //Open AddBook Activity
        mAddBook = (Button) findViewById(R.id.add_book);
        mAddBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent addBook = new Intent(MainActivity.this, AddBook.class);
                int requestCode = 1;
                startActivityForResult(addBook, requestCode);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String addAuthor = data.getStringExtra("addAuthor");
        String addTitle = data.getStringExtra("addTitle");
        String addRating = data.getStringExtra("addRating");
        String addIsbn = data.getStringExtra("addIsbn");
        String addSeries = data.getStringExtra("addSeries");

        if(addAuthor != null){
            //Add variables from AddBook activity to new Book object
            books.add(new Book(addAuthor, addTitle, 5, addIsbn, addSeries));
            bookAdapter.notifyDataSetChanged();
        }

    }
}
在您的adBookActivity中,请确保在关闭活动之前致电setResult(1, data)

*将1(请求代码)更改为常量,以使代码更具可读性

答案 1 :(得分:0)

我认为这是使用sqlite DB的最佳方式。并从sqlite db表中设置列表框项目,它将快速运行您的应用程序。