我在我的应用程序中有一个激活,它使用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);
}
}
答案 0 :(得分: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
,它会以不同的额外内容开始活动。