android编程学习中,最近在编写一个简单的android项目,实现在TwoActivity(书籍列表)中点击LisView的任意item传递书名,给ThreeAvtivity(章节列表),通过接收到的数据(书名)确定显示书的章节目录,使用的是intent方法。其中TwoActivity中有6本书的List,ThreeActivity中每本书有十个章节
Intent 可以启动一个Activity,也可以在启动Activity的时候传递数据。intent中提供了putExtra()方法,它可以把我们想要传递的数据暂存在intent中,启动了另一个Activity后,通过getIntent().getStringExtra()(其中getStringExtra()通过活动传递的数据类型决定是String或是其他类型),再从 Intent中取出。
本次只使用了intent的简单传递数据的方法,对于Serializable与Parcelable接口的使用,等以后真正使用后再记录。本次实例的代码如下:
activity_two.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">Z <ListView android:id="@+id/lv_books" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_margin="@dimen/dimen_book_margin" android:background="@color/colorGray"/> </LinearLayout> item_book: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#8f8f8f"> <TextView android:id="@+id/tv_book_name" android:layout_width="match_parent" android:layout_height="40dp" /> <TextView android:id="@+id/tv_book_price" android:layout_width="match_parent" android:layout_height="40dp" /> </LinearLayout>
activity_three.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/three_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="@dimen/title_margintop" android:text="目录" android:textSize="@dimen/title" android:textColor="@color/title"/> <ListView android:id="@+id/lv_chapter" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="@dimen/dimen_book_margin"/> </LinearLayout>
item_chapter:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffeeee"> <TextView android:id="@+id/tv_chapter_number" android:layout_width="match_parent" android:layout_height="20dp"/> <TextView android:id="@+id/tv_chapter_title" android:layout_width="match_parent" android:layout_height="20dp"/> <TextView android:id="@+id/tv_chapter_page" android:layout_width="match_parent" android:layout_height="20dp"/> </LinearLayout>
TwoActivity.java
public class TwoAcitivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); TextView tvTitle = (TextView) findViewById(R.id.two_title); tvTitle.setText(mykey0); final String[] names = {"Java", "Android", "JSP", "C/C++", "C#", "Lua"}; int[] prices = new int[]{100, 50, 80, 60, 70, 89}; final ArrayList<Book> mList = new ArrayList<>(); for(int i=0;i<50;i++){ Book mBook = new Book(names[i%6], i, prices[i%6]); mList.add(mBook); } ListView lvBooks = (ListView) findViewById(R.id.lv_books); BooksAdapter mBooksAdapter = new BooksAdapter(TwoAcitivity.this, mList); lvBooks.setAdapter(mBooksAdapter); lvBooks.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(TwoAcitivity.this,ThreeActivity.class); intent.putExtra("bookName", mList.get(position).getName());//传递数据,关键字为“bookName”,传递的是书名 startActivity(intent); } }); } }
ThreeActivity.java:
public class ThreeActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_three); Intent dataIntent = getIntent();//接收传递的数据 String bookName = dataIntent.getStringExtra("bookName"); BookDetail mBookDetail = null; switch (bookName) { case "Java": String[] numbers = this.getResources().getStringArray(R.array.chapter_number_java); String[] titles = this.getResources().getStringArray(R.array.title_java); int[] pages = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; mBookDetail = new BookDetail(numbers, titles, pages); break; case "Android": String[] numbers1 = this.getResources().getStringArray(R.array.chapter_number_android); String[] titles1 = this.getResources().getStringArray(R.array.title_android); int[] pages1 = new int[]{1, 5, 11, 15, 33, 34, 45, 56, 67, 78}; mBookDetail = new BookDetail(numbers1, titles1, pages1); break; case "JSP": String[] numbers2 = this.getResources().getStringArray(R.array.chapter_number_jsp); String[] titles2 = this.getResources().getStringArray(R.array.title_jsp); int[] pages2 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; mBookDetail = new BookDetail(numbers2, titles2, pages2); break; case "C/C++": String[] numbers3 = this.getResources().getStringArray(R.array.chapter_number_C1); String[] titles3 = this.getResources().getStringArray(R.array.title_C1); int[] pages3 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; mBookDetail = new BookDetail(numbers3, titles3, pages3); break; case "C#": String[] numbers4 = this.getResources().getStringArray(R.array.chapter_number_C2); String[] titles4 = this.getResources().getStringArray(R.array.title_C2); int[] pages4 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; mBookDetail = new BookDetail(numbers4, titles4, pages4); break; case "Lua": String[] numbers5 = this.getResources().getStringArray(R.array.chapter_number_Lua); String[] titles5 = this.getResources().getStringArray(R.array.title_Lua); int[] pages5 = new int[]{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; mBookDetail = new BookDetail(numbers5, titles5, pages5); break; } ListView lvChapter = (ListView) findViewById(R.id.lv_chapter); ChapterAdapter mChapterAdapter = new ChapterAdapter(ThreeActivity.this, mBookDetail.getmChapterList()); lvChapter.setAdapter(mChapterAdapter); } }