2015-02-24 44 views
0

我没有线索如何做到这一点。我已经尝试过使用数组,但无法设法让它工作。甚至试图从数据库获取ID,然后将其传递给下一个活动,然后从数据库调用它来获取文本,但再次无法设法使其工作。我猜数组是我的弱点。有人能帮助我吗?将字符串列表传递给另一个活动并显示它们! Android

在主要活动中有很多从数据库中提取的单词,我需要在第二个活动中显示它们。你是怎样做的?

下面是从1活动代码:

public class Game extends Activity implements SensorEventListener { 
    int busy = 0; 
    int nagni = 0; 
    static TextView word; 
    private CountDownTimer countDownTimer; 
    private boolean timerHasStarted = false; 
    public TextView text; 
    private final long startTime = 60 * 1000; 
    private final long interval = 1 * 1000; 
    int cat4; 
    private SensorManager sManager; 
    Sensor accelerometer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
     //senzor gibanja 
     sManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
     accelerometer=sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     sManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); 

     text = (TextView) this.findViewById(R.id.timer2); 
     countDownTimer = new MyCountDownTimer(startTime, interval); 
     text.setText(text.getText() + String.valueOf(startTime/1000)); 
     Intent intent = getIntent(); 
     cat4 = intent.getIntExtra("cat3", 0); 

     // Font path 
     String fontPath = "AdventureRR.otf"; 

     // text view label 
     TextView ourText3 = (TextView) findViewById(R.id.textView3); 

     // Loading Font Face 
     Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

     // Applying font 

     ourText3.setTypeface(tf); 


     set(); 

     countDownTimer.start(); 
     timerHasStarted = true; 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_game, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void set(){ 
     SQLiteDatabase db; 


     String que = ""; 
     db=openOrCreateDatabase("baza.db",MODE_PRIVATE, null); 


     Cursor cd=db.rawQuery("SELECT * FROM question WHERE cat=" + cat4 + " AND used = 1", null); 
     cd.moveToFirst(); 

     que=cd.getString(cd.getColumnIndex("text")); 

     //patch 
     String id2=(cd.getString(cd.getColumnIndex("id"))); 
     int idd2=Integer.parseInt(id2); 
     db.execSQL("UPDATE question SET used=0 WHERE id ="+idd2+";"); 


     word = (TextView) findViewById(R.id.textView3); 
     word.setText(""); 
     word.setText(que); 


     db.close(); 
     busy = 0; 
    } 




    @Override 
    public void onSensorChanged(SensorEvent event) { 


     //else it will output the Roll, Pitch and Yawn values 
     if (event.values[2]>8){ 
      if (busy == 0){ 

       if (nagni == 0){ 

       Intent i = new Intent(this,Pass.class); 
       startActivityForResult(i, 1); 

       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
       @Override 
        public void run() { 
         //Do something after 1000ms 
         set(); 
        } 
       }, 1000); 
       } 
      } 

      busy = 1; 

     } 
     else if(event.values[2]<-8){ 

      if (busy == 0){ 

       if (nagni == 0){ 


       Intent i = new Intent(this,Correct.class); 
       startActivityForResult(i, 1); 

       final Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         //Do something after 1000ms 
         set(); 
        } 
       }, 1000); 
       } 
      } 
      busy = 1; 

     } 

     else{ 
      busy = 0; 
     } 

    } 

    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 

    } 

    public class MyCountDownTimer extends CountDownTimer { 
     public MyCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     @Override 
     public void onFinish() { 
      text.setText("END!"); 

      nagni=1; 

      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 

        Intent i = new Intent(Game.this, Results.class); 
        startActivity(i); 

       } 
      }, 1500); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      if (millisUntilFinished>10000){ 
      text.setText("0:" + millisUntilFinished/1000); 
      } 
      else if (millisUntilFinished<10000){ 
      text.setText("0:0" + millisUntilFinished/1000); 
      } 


     } 
    } 

    @Override 
    public void onBackPressed() { 
     // do nothing. 
    } 



} 

这里是我的2活动:

public class Results extends Activity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_results); 

     final ImageButton one = (ImageButton) findViewById(R.id.imageButton); 
     final ImageButton two = (ImageButton) findViewById(R.id.imageButton2); 


     one.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent= new Intent(Results.this, Main.class); 
       startActivity(intent); 
      } 
     }); 

     two.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent= new Intent(Results.this, Instructions.class); 
       startActivity(intent); 
      } 
     }); 


     // Font path 
     String fontPath = "AdventureRR.otf"; 

     // text view label 
     TextView ourText = (TextView) findViewById(R.id.textView); 

     // Loading Font Face 
     Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 

     // Applying font 

     ourText.setTypeface(tf); 





    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_results, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onBackPressed() { 
     // do nothing. 
    } 

回答

1

虽然从明年开始活动意图添加列表如下

intent.putStringArrayListExtra("data", list); 

并在下一个活动中使用它如下

getIntent().getStringArrayListExtra("data"); 

希望这会帮助你。

0

你应该在你的第一个活动,intent.getExtra()在第二活动

这里使用intent.putExtra是一个例子:

活性1

intent.putExtra(tag, stringArray); 

活性2

getIntent().getExtras().getStringArray(key); 
相关问题