0

我的应用程序中有一个 activty,它使用 putextra(int) 方法在启动 aactivty 时首先传递值 0。然后在按下下一个按钮时,它会传递 6,然后是 12,依此类推。但问题在于第二次按下时我发现使用 getextras 方法收到的索引值为 0。是不是因为我是从自身调用活动。这是代码片段:

package com.movie;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.Window;
  import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.Toast;

 public class movielist extends Activity implements OnClickListener{

Button bm[] = new Button[6];
Button nxt,prev;
String namesdb[]=new String[50];
databaseconnect db;
String lang;
int start,index,end;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.mallu_list);
    db = new databaseconnect(this);
    db.open();

    start=this.getIntent().getExtras().getInt("index");
     lang =this.getIntent().getExtras().getString("lang");

     Toast.makeText(this, start+"", Toast.LENGTH_SHORT).show();
     Log.i("info",start+"");

    if(!db.isdata(lang))
    {
        Toast.makeText(this, "No data in Database", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    end=start+6;
    bm[0] = (Button) findViewById(R.id.bm1);
    bm[1] = (Button) findViewById(R.id.bm2);
    bm[2] = (Button) findViewById(R.id.bm3);
    bm[3] = (Button) findViewById(R.id.bm4);
    bm[4] = (Button) findViewById(R.id.bm5);
    bm[5] = (Button) findViewById(R.id.bm6);

    nxt = (Button) findViewById(R.id.nxt);
    prev = (Button) findViewById(R.id.prev);
    if(start==0)
    {
//      prev.setBackgroundResource(0);
//      prev.setText("");
    }
    else
        prev.setOnClickListener(this);


    namesdb = db.getmovie("",lang);
    for (int i = 0; start+i < namesdb.length && i<6; i++) {
        bm[i].setText(namesdb[start+i]);
        bm[i].setOnClickListener(this);
    }
    int flag=0;
    int i=namesdb.length;
    Log.i("info",i+"");
        Toast.makeText(this, i+"", Toast.LENGTH_SHORT).show();
        for(int j=5;j>=i-start;j--)
        {
            bm[j].setBackgroundResource(0);
            flag=1;
        }
    if(flag==1)
    {
//      nxt.setBackgroundResource(0);
//      nxt.setText("");
    }
    else
        nxt.setOnClickListener(this);
  }
 @Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Intent myint2 = new Intent(this, list.class);

    startActivity(myint2);
}




@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bm1:
        call(namesdb[start],start,lang);
        break;
    case R.id.bm2:
        call(namesdb[start+1],start,lang);
        break;
    case R.id.bm3:
        call(namesdb[start+2],start,lang);
        break;
    case R.id.bm4:
        call(namesdb[start+3],start,lang);
        break;
    case R.id.bm5:
        call(namesdb[start+4],start,lang);
        break;
    case R.id.bm6:
        call(namesdb[start+5],start,lang);
        break;

    case R.id.nxt:
        int i=namesdb.length;
        Intent myint2 = new Intent(this,movielist.class);

    myint2.putExtra("index",end);
    myint2.putExtra("lang",lang);
    startActivity(myint2);
    case R.id.prev:
        if(start==0)
        {
            prev.setBackgroundResource(0);
        }
        else
        {
         myint2 = new Intent(this,movielist.class);

    myint2.putExtra("index",start-6);
    myint2.putExtra("lang",lang);
    startActivity(myint2);}
        break;

    }

}

public void call(String name,int start2,String lang) {

    Intent myint = new Intent(this, detail.class);
    myint.putExtra("nameid", name);
    myint.putExtra("index", start2);
    myint.putExtra("lang", lang);

    startActivity(myint);
}



}
4

1 回答 1

1

这是你的问题。声明的这一部分switch

case R.id.nxt:
    int i=namesdb.length;
    Intent myint2 = new Intent(this,movielist.class);
    myint2.putExtra("index",end);
    myint2.putExtra("lang",lang);
    startActivity(myint2);

最后缺少一个break。所以它一直到下一个case,它以不同的额外内容开始活动。

于 2013-04-18T18:36:28.183 回答