4 Replies - 1311 Views - Last Post: 06 September 2015 - 02:15 PM

#1 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Pass data from one activity to another Android

Posted 06 September 2015 - 02:49 AM

Can you help me. I have developed a app with a quiz. When the quiz is finished a new activity (result) appears showing the score. I want to pass the score to a new activity namely scores. If i go to the menu and click on scores the score activity will open and then the score of the quiz must be showed. Here is my code. What is wrong. The score must be showed in a textview.

Result.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class Result extends Activity
{
 
    private static Button playbtn;
    private static Button menubutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        onclickPlayButtonListener();
        onclickMenuButtonListener();
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        int score = b.getInt("score");
        textResult.setText("You scored" + " " + score + " for the quiz.");
        Intent i = new Intent(getApplicationContext(), Result.class);
        i.putExtra("somevariable",score);
        startActivity(i);
    }
 
    public void onclickPlayButtonListener() {
        playbtn = (Button) findViewById(R.id.btn);
        playbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }
 
    public void onclickMenuButtonListener() {
        menubutton = (Button) findViewById(R.id.menubtn);
        menubutton.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                      }
                }
        );


Scores.java
import android.view.MenuItem;
import android.widget.TextView;
import android.content.Intent;
 
public class Scores extends ActionBarActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        Intent intent = getIntent();
        String value = intent.getStringExtra("somevariable");
        TextView txtScore1 = (TextView) findViewById(R.id.txtScore1);
        txtScore1.setText("You scored" + " " + value + " for the quiz.");
    }
 
    @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_scores, 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);
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Pass data from one activity to another Android

#2 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass data from one activity to another Android

Posted 06 September 2015 - 10:09 AM

Had to change Result.class to Scores.class

Intent i = new Intent(getApplicationContext(), Scores.class);

Can someone maybe tell me why it shows that I have scored null for the quiz but for instance with the one try I got four correct? And during the quiz it shows score :4
Was This Post Helpful? 0
  • +
  • -

#3 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Pass data from one activity to another Android

Posted 06 September 2015 - 01:41 PM

where are your other files ? also you have syntax errors..
Was This Post Helpful? 0
  • +
  • -

#4 parkie02   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 15-May 13

Re: Pass data from one activity to another Android

Posted 06 September 2015 - 01:51 PM

Other files

MainActivity:
package app.mobiledevicesecurity;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    DatabaseHelper myDb;
    private static Button readbtn;
    private static Button quizbtn;
    private static Button scoresbtn;
    private static Button settingsbtn;
    private static Button helpbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new DatabaseHelper(this);
        myDb.insertData();

         onclickReadButtonListener();
         onclickQuizButtonListener();
         onclickScoresButtonListener();
         onclickSettingsButtonListener();
         onclickHelpButtonListener();


    }

    public void onclickReadButtonListener() {
        readbtn = (Button) findViewById(R.id.readbutton);
        readbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Read_Category");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickQuizButtonListener() {
        quizbtn = (Button) findViewById(R.id.quizbutton);
        quizbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Quiz");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickScoresButtonListener() {
        scoresbtn = (Button) findViewById(R.id.scoresbutton);
        scoresbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Scores");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickSettingsButtonListener() {
        settingsbtn = (Button) findViewById(R.id.settingsbutton);
        settingsbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Settings");
                        startActivity(intent);
                    }
                }
        );
    }

    public void onclickHelpButtonListener() {
        helpbtn = (Button) findViewById(R.id.helpbutton);
        helpbtn.setonclickListener(
                new View.onclickListener() {
                    @Override
                    public void onclick(View v) {
                        Intent intent = new Intent("app.mobiledevicesecurity.Help");
                        startActivity(intent);
                    }
                }
        );


    }

    @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_main, 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);
    }
}


Quiz:
package app.mobiledevicesecurity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Quiz extends Activity
{
    List<Question> questionList;
    int score = 0;
    int qid = 0;
    Question currentQuest;
    TextView txtQuestion, scored;
    Button button1, button2, button3;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        QuizHelper db = new QuizHelper(this);
        questionList = db.getAllQuestions();
        currentQuest = questionList.get(qid);
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        scored = (TextView) findViewById(R.id.score);



        setQuestionView();

        button1.setonclickListener(new View.onclickListener()
        {
            @Override
            public void onclick(View v) {
               getAnswer(button1.getText().toString());
            }
        });

        button2.setonclickListener(new View.onclickListener()
        {
            @Override
            public void onclick(View v) {
                getAnswer(button2.getText().toString());
            }
        });

        button3.setonclickListener(new View.onclickListener()
        {
            @Override
            public void onclick(View v) {
                getAnswer(button3.getText().toString());
            }
        });
    }
    public void getAnswer(String AnswerString)
    {
        if (currentQuest.getAnswer().equals(AnswerString))
        {
            score++;
            scored.setText("Score : " + score);
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(B)/>/>;
            startActivity(intent);
            finish();
        }
        if (qid < questionList.size()) {
            currentQuest = questionList.get(qid);
            setQuestionView();
        }
        else
        {

            Intent intent = new Intent(Quiz.this,
                    Result.class);
            Bundle b = new Bundle();
            b.putInt("score", score);
            intent.putExtras(B)/>/>;
            startActivity(intent);
            finish();
        }
    }

    private void setQuestionView()
    {
        txtQuestion.setText(currentQuest.getQuest());
        button1.setText(currentQuest.getOption1());
        button2.setText(currentQuest.getOption2());
        button3.setText(currentQuest.getOption3());
        qid++;
    }
}


QuizHelper:
package app.mobiledevicesecurity;

/**
 * Created by Martin on 2015/08/28.
 */
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class QuizHelper extends SQLiteOpenHelper
{
    private static final int VERSION = 1;
    private static final String DATABASE_NAME = "secquiz";
    private static final String TABLE_QUEST = "questions";

    private static final String ID = "id";
    private static final String QUEST = "question";
    private static final String ANSWER = "answer";
    private static final String OPT1 = "opt1";
    private static final String OPT2 = "opt2";
    private static final String OPT3 = "opt3";
    private SQLiteDatabase database;

    public QuizHelper(Context context)
    {
        super(context, DATABASE_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        database = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUEST
                + " TEXT, " + ANSWER + " TEXT, " + OPT1 + " TEXT, "
                + OPT2 + " TEXT, " + OPT3 + " TEXT)";
        db.execSQL(sql);
        addQuestion();
    }

    private void addQuestion() {
        Question q1 = new Question("1+1", "1", "2", "3", "2");
        this.addQuestion(q1);
   
        // END
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV)
    {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        onCreate(db);
    }

    public void addQuestion(Question quest)
    {
        ContentValues values = new ContentValues();
        values.put(QUEST, quest.getQuest());
        values.put(ANSWER, quest.getAnswer());
        values.put(OPT1, quest.getOption1());
        values.put(OPT2, quest.getOption2());
        values.put(OPT3, quest.getOption3());
        database.insert(TABLE_QUEST, null, values);
    }

    public List<Question> getAllQuestions()
    {
        List<Question> List = new ArrayList<Question>();
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        database = this.getReadableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.moveToFirst())
        {
            do
            {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQuest(cursor.getString(1));
                quest.setAnswer(cursor.getString(2));
                quest.setOption1(cursor.getString(3));
                quest.setOption2(cursor.getString(4));
                quest.setOption3(cursor.getString(5));
                List.add(quest);
            }
            while (cursor.moveToNext());
        }
        return List;
    }
}



AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.mobiledevicesecurity" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name=".SplashScreen"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Read_Category"
            android:label="@string/title_activity_read__category" >
            <intent-filter>
                <action android:name="app.mobiledevicesecurity.Read_Category" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Quiz"
            android:label="@string/title_activity_quiz" >
            <intent-filter>
                <action android:name="app.mobiledevicesecurity.Quiz" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Scores"
            android:label="@string/title_activity_scores" >
            <intent-filter>
                <action android:name="app.mobiledevicesecurity.Scores" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Settings"
            android:label="@string/title_activity_settings" >
            <intent-filter>
                <action android:name="app.mobiledevicesecurity.Settings" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Help"
            android:label="@string/title_activity_help" >
            <intent-filter>
                <action android:name="app.mobiledevicesecurity.Help" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Result"
            android:label="@string/title_activity_result" >
        </activity>
    </application>

</manifest>


what syntax errors are there. I am new to app development. Learning it for myself? Thanks for the reply. I added the other files.
Was This Post Helpful? 0
  • +
  • -

#5 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Pass data from one activity to another Android

Posted 06 September 2015 - 02:15 PM

onclickListener is the syntax error in class Result.Im also new to android development so without debugging the app its hard to tell what is wrong.To run this app all files should be included..xml files are missing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1