开发者

How should this be achieved? Activity/ActivityGroup or ViewFlipper?

开发者 https://www.devze.com 2023-01-17 10:23 出处:网络
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.

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):

        On开发者_如何学PythonItemClickListener 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!


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消