0

I am making an android app. this app consist of list. by clicking each item of list a new page opens. this list contains 50 items.I should make 50 activity and their corresponding XML file. so, is there any way that make this process easier that don't force me to make all this 50 activity one by one? my reputation is not enough. so I upload related picture. http://uupload.ir/files/oh1o_1.png http://uupload.ir/files/zam9_2.png

abcsd
  • 5
  • 5

3 Answers3

2

Not sure what you mean by creating 50 pages. You have NOT to create a new class for each item in the list, instead you will create a new instance of a class. This is what programming, and OOP (Object Oriented Programming) comes into play.. Basically what you need to do is:

  • Create a "main_activity" class which will contain your list
  • Create a "item_detail_activity" which will show the details about your clicked item

What you need to write is the logic of "passing" the correct data depending on the clicked item. When an item in the "main_activity" is clicked, you will create a new instance of "item_detail_activity", passing the correct data (through a bundle).

BTW there are a lot of tutorials out there that will help you understand better the logic of an Android application.

Nickeat
  • 980
  • 6
  • 6
  • Or even the logic of an OOP application – Phantomazi Aug 25 '15 at 08:33
  • thanks. I have main activity and by clicking each item in this activity new activity should start. so I understand that I should to use some thing that pass new data to new activity and I don't need to make this 50 activity separately. – abcsd Aug 25 '15 at 09:06
  • Here you can find an example of how to use a Bundle for passing data between activities: [bundle_example](http://stackoverflow.com/questions/14876273/simple-example-for-intent-and-bundle) – Nickeat Aug 25 '15 at 09:23
0

If you have a list of items probably means that you have a list of objects which are of the same kind with same structure.

Due to this, you will have 50 pages which show to the user the same item with different values to variables, with the same structure.

You can make the objects in your list parcelable (refer to this), then pass it with the Intent to a second Activity you create, receive it, and at last populate the screen with the item you passed.

If you have troubles or doubts feel free to ask :)

EDIT: Imagine your listview is called ListView, and the List of items you used to fill the listview is called list in the MainActivity do this:

ListView listView = (ListView) findViewById(R.Id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
          // position is the index of selected item
          // Launching new Activity on selecting single List Item
          Intent i = new Intent(MainActivity.this, SecondActivity.class);
          // sending data to new activity
          i.putExtra("item", list.get(position));
          startActivity(i);
      }
  });

in your SecondActivity, with getParcelableExtra("item") you retreieve the item clicked. Here with the variables of this item you can populate the page.

here is the doc for intents.

In secondActivity, if something must disappear if a variable has a certain value or is null, play with visibility or fragment: create an Activity with all case shown, than work with visibility or with fragments and adjust it ;)

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • thanks you know I have a listview in the main activity. by clicking each item a new activity should start. I used intent to start new activity. but this list contain 50 item and each item refers to different pages. I mean that each of there new activity contains different content. should I create this new activities are is there any way that can only pass the new data to that pages? – abcsd Aug 25 '15 at 09:10
  • I will try Parcelable. each new page contain different image view and text. I hope I can do it – abcsd Aug 25 '15 at 09:31
0

Change your activity to fragment activity and write code to display the details corresponding listview click. it easy to follow.

anbu selvan
  • 124
  • 2
  • 8