将字符串列表传递给另一个活动并显示它们! Android的

时间:2015-02-24 12:53:13

标签: android arrays string listview

我不知道如何做到这一点。我尝试使用数组,但无法设法让它工作。甚至试图从数据库中获取ID然后将其传递给下一个活动,然后从数据库中调用它来获取文本,但是再次无法设法让它工作。我认为阵列是我的弱点。有人可以帮帮我吗?

在主要活动中有很多单词来自数据库,我需要在第二个活动中显示它们。你是怎么做到的?

以下是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.
    }

2 个答案:

答案 0 :(得分:1)

启动意向添加列表的下一个活动时如下

intent.putStringArrayListExtra("data", list);

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

getIntent().getStringArrayListExtra("data");

希望这会对你有所帮助。

答案 1 :(得分:0)

您应该在第一个活动中使用intent.putExtra,在第二个活动中使用intent.getExtra()

以下是一个例子:

<强>活性1

intent.putExtra(tag, stringArray);

<强>活性2

getIntent().getExtras().getStringArray(key);