tags:

views:

19

answers:

1

Hello! I have a ListView with let's say 5 items. When I click on one of these it automatically start a new activity based on the position of my ListView.

Then, in the new activity, it shows like "1 of 5".

I have two buttons (back and next) to show 2 of 5 etc, but how should I implement so it loads new content without starting a lot of activites for every "2 of 5", "3 of 5" etc..? It's meant to be done this way so the user don't need to go back to the ListView and then choose the second position..

My current code (From the ListView):

        OnItemClickListener itemListener = new OnItemClickListener() {  
        public void onItemClick(AdapterView<?> parent, View v,
          int position, long rowid) {
            Intent intent = new Intent().setClass(ctx, Details.class);
            Bundle b = new Bundle();
            b.putInt("id", parent.getPositionForView(v));
            intent.putExtras(b);
            startActivity(intent);      
        }
    };

and then a piece of code in Details.java:

       next = (Button)findViewById(R.id.next_button);
       back = (Button)findViewById(R.id.back_button);
       Bundle b = getIntent().getExtras();
       id = b.getInt("id");
       id_header_sum = id+1;
       String string_id = Integer.toString(id_header_sum);
       one_of_all.setText(string_id + " of 5");

                    nextOnClick = new OnClickListener() {
            public void onClick(View arg0) {
                  if(id_header_sum==30){
                   }
               else {

               }
            }
        };

        backOnClick = new OnClickListener() {
            public void onClick(View arg0) {
                   if(id_header_sum==1){
                   }
                   else {

                   }
            }
        };

        next.setOnClickListener(nextOnClick);     
        back.setOnClickListener(backOnClick); 

I don't want to start a single activity for every detail.

Please tell me if the question is unclear and thanks in advance!

+1  A: 

You can try using Application class.

Base class for those who need to maintain global application state.

You can leave the list of elements there. Example:

// Be careful, the name of this class must be
// placed in the / said in your manifest
// <manifest xmlns:android="http://schemas.android.com/apk/res/android"
//     package="com.something.appname"
// and be called as the manifest says
//    <application android:name="YourAppName"

public class YourAppName extends Application {
  private List<Item> mItems;

  @Override
  public void onCreate() {
    super.onCreate();
    mItems = getYourItemList();
  }

  public List<Item> getItems() {
    return mItems;
  }

}

Nothing fancy there. Just a getter for the list.

The Detail Activity can be something like this:

public class DetailActivity extends Activity {
  private YourAppName mApp;
  private ImageButton mNextButton;
  private ImageButton mBackButton;
  private TextView mTitle;
  private int mIndex;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    mApp = (YourAppName) getApplication();

    /*This index should come from the intent */
    mIndex = 2; 
    getWidgets();
    populateWidgets();
  }

  private void getWidgets() {
    mNextButton = (ImageButton)findViewById(R.id.next);
    mTitle = (TextView)findViewById(R.id.title);
    /* other findViewById */

    mNextButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
           mIndex++; /*Notice that this will get a NPE.
                     You need to place a better logic here */
           populateWidgets();
        }
    });
  }

  private void populateWidgets() {
    Item item = mApp.getItemList().get(mItem);
    mTitle.setText(item.getName());
  }
}

Some comments about your code:

Intent intent = new Intent().setClass(ctx, Details.class); looks strange. The common way would be Intent intent = new Intent(ctx, Details.class);

Macarse
How should I implement it? I already have necesseary stuff in my onCreate.
Julian Assange
Put the list that populates the `ListView` in the class that extends `Application` and use that class to get it's items.
Macarse
Hm, I'm not that familiar with this, can you provide a small example or something?
Julian Assange
@Julian Assange: I just added an example.
Macarse
Okay, I think I understand now. In my Details.java I can now get the list by calling for YourAppName class. and when I'm clicking on the next button I call for it once again so it loads the new one?
Julian Assange
Having YourAppName class gives you direct access to list, so if you started on index 1, with some logic you can get the following in the list and repopulate your view.
Macarse
Okey, but let's say now, that I'm on the page 4 of 5 in my Details.java. If I click on the back button I want it to load the page 3 of 5 but still be in Details.java. Should I put mApp.getItems(); in backOnClick? Sorry for bringing this much noise...
Julian Assange
@Julian: You can start a new `Activity` from Details to Details. I wouldn't recommend it. If you are browsing detailed items it would nice to stay in the same activity.
Macarse
Yes, and it is this I trying to achieve. I'm not sure how to reload it, anyways thanks for your help, I really appreciate it.
Julian Assange
@Julian: Let me add something else in my example.
Macarse
@Macarse: Thanks for your support!
Julian Assange
@Julian: np. Notice that I didn't test that code. It might have syntax errors :(
Macarse
Wow, really really thank you! I'm sure this will work!
Julian Assange